코딩로그
[백준/BOJ/C++] 4606번 The Seven Percent Solution 본문
[문제]
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 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.
Charater | 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.
[출력]
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 "A").
[Source Code]
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main() {
while(true){
string input;
getline(cin, input);
if(input == "#") break;
vector<string> vector;
for(int i = 0; i < input.length(); i++){
if(isspace(input[i]) != 0)
vector.push_back("%20");
else if(input[i] == '!')
vector.push_back("%21");
else if(input[i] == '$')
vector.push_back("%24");
else if(input[i] == '%')
vector.push_back("%25");
else if(input[i] == '(')
vector.push_back("%28");
else if(input[i] == ')')
vector.push_back("%29");
else if(input[i] == '*')
vector.push_back("%2a");
else{
string temp(1, input[i]);
vector.push_back(temp);
}
}
for(int i = 0; i < vector.size(); i++){
cout << vector[i];
}
cout << "\n";
}
}
[결과 화면]
'YJ > C++' 카테고리의 다른 글
[백준/BOJ/C++] 12526번 Centauri Prime (Small2) (0) | 2021.11.05 |
---|---|
[백준/BOJ/C++] 12525번 Centauri Prime (Small1) (0) | 2021.11.05 |
[백준/BOJ/C++] 15178번 Angles (0) | 2021.11.05 |
[백준/BOJ/C++] 13297번 Quick Estimates (0) | 2021.11.05 |
[백준/BOJ/C++] 9947번 Coin tossing (0) | 2021.11.05 |