Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

코딩로그

[백준/BOJ/C++] 4606번 The Seven Percent Solution 본문

YJ/C++

[백준/BOJ/C++] 4606번 The Seven Percent Solution

Team DAON 2021. 11. 5. 14:29

[문제]

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";
	}
}

[결과 화면]