2014
02-14

# The Seven Percent Solution

Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/, mailto:[email protected], ftp://127.0.0.1/pub/linux, or even just readme.txt that are used to identify a resource, usually on the Internet or a local computer. Certain characters are reserved within URIs, and if a reserved character is part of an identifier then it must be percent-encoded by replacing it with a percent sign followed by two hexadecimal digits representing the ASCII code of the character. A table of seven reserved characters and their encodings is shown below. Your job is to write a program that can percent-encode a string of characters.

Character  Encoding
" " (space)  %20
"!" (exclamation point)  %21
"$" (dollar sign) %24 "%" (percent sign) %25 "(" (left parenthesis) %28 ")" (right parenthesis) %29 "*" (asterisk) %2a 输入: The input consists of one or more strings, each 1�79 characters long and on a line by itself, followed by a line containing only "#" that signals the end of the input. The character "#" is used only as an end-of-input marker and will not appear anywhere else in the input. A string may contain spaces, but not at the beginning or end of the string, and there will never be two or more consecutive spaces. 输出: The input consists of one or more strings, each 1�79 characters long and on a line by itself, followed by a line containing only "#" that signals the end of the input. The character "#" is used only as an end-of-input marker and will not appear anywhere else in the input. A string may contain spaces, but not at the beginning or end of the string, and there will never be two or more consecutive spaces. 样例输入: Happy Joy Joy! http://icpc.baylor.edu/icpc/ plain_vanilla (**) ? the 7% solution # 样例输出: Happy%20Joy%20Joy%21 http://icpc.baylor.edu/icpc/ plain_vanilla %28%2a%2a%29 ? the%207%25%20solution 题意：给一些字符串，把字符串输出。如果有题目里的那7种字符，按%数值的形式输出。 代码： # include <stdio.h> # include <string.h> char str[100] ; char t[] = " !$%()*" ;
int p[] = {0x20, 0x21, 0x24, 0x25, 0x28, 0x29, 0x2a} ;

int main ()
{
int i, j ;
while (gets (str))
{
if (!strcmp(str, "#")) break ;
for (i = 0 ; str[i] ; i++)
{
for (j = 0 ; j < 7 ; j++) if (str[i] == t[j])
{
printf ("%%%x", p[j]) ;
break ;
}
if (j >= 7) printf ("%c", str[i]) ;
}
puts ("") ;
}
return 0 ;
}