Zoj problem set-2932 The seven percent solution
Time limit:1 second Memory limit:32768 KB
uniform resource identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/ , mailto: foo@bar.org , 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 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 |
Input
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 in spaces, but not at the beginning or end of the string, and there will never be two or more consecutive spaces.
Output
For each input string, replace every occurrence of a reserved character in the Table above by its percent-encoding, exactly as shown, and output the resulting string on a line by itself. note that the percent-encoding for an asterisk is % 2a (with a lowercase "A") rather than % 2a (with an uppercase "").
Sample Input
Happy Joy! Http://icpc.baylor.edu/icpc/plain_vanilla (**)? The 7% solution #
Sample output
Happy % 20joy % 20joy % 21 http://icpc.baylor.edu/icpc/plain_vanilla%28%2a%2a%29? The % 207% 25% 20 Solution
Source:
The 2007 ACM mid-Central USA Programming Contest
Source code:
# Include < Iostream >
# Include < String >
# Include < Sstream >
# Include < Limits >
Using Namespace STD;
Int Main ()
{
String S;
While (Getline (CIN, S) && S ! = " # " )
{
Char C;
For ( Int I = 0 ; I < S. Length (); I ++ )
{
C = S [I];
Switch (C)
{
Case ' ' :
Cout < " % 20 " ;
Break ;
Case ' ! ' :
Cout < " % 21 " ;
Break ;
Case ' $ ' :
Cout < " % 24 " ;
Break ;
Case ' % ' :
Cout < " % 25 " ;
Break ;
Case ' ( ' :
Cout < " % 28 " ;
Break ;
Case ' ) ' :
Cout < " % 29 " ;
Break ;
Case ' * ' :
Cout < " % 2a " ;
Break ;
Default :
Cout < C;
Break ;
}
}
Cout < Endl;
}
Return 0 ;
}