Question link:
PKU: http://poj.org/problem? Id = 3650
Zju: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 1931
HDU: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2719
Description
Uniform resource identifiers (or URIs) are strings likeHttp://icpc.baylor.edu/icpc/,Mailto: [email protected],FTP: // 127.0.0.1/pub/Linux, Or even justReadme.txtThat 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 bePercent-encodedBy 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 |
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 Joy!http://icpc.baylor.edu/icpc/plain_vanilla(**)?the 7% solution#
Sample output
Happy%20Joy%20Joy%21http://icpc.baylor.edu/icpc/plain_vanilla%28%2a%2a%29?the%207%25%20solution
Source
Mid-Central USA 2007
The Code is as follows:
#include <cstdio>#include <cstring>int main(){ char s[1000]; while(gets(s)) { int len = strlen(s); if(s[0] =='#' && len == 1) break; for(int i = 0; i < len; i++) { if(s[i]==' ') printf("%%20"); else if(s[i] == '!') printf("%%21"); else if(s[i] == '$') printf("%%24"); else if(s[i] == '%') printf("%%25"); else if(s[i] == '(') printf("%%28"); else if(s[i] == ')') printf("%%29"); else if(s[i] == '*') printf("%%2a"); else printf("%c",s[i]); } printf("\n"); } return 0;}
Poj 3650 & zju 2932 & HDU 2719 the seven percent solution (simulation)