/*문자열 안에 있는 uri코드를 변환하라.*/
#include <iostream>
#include <string>
#include <stdlib.h> //strtol() 사용하기 위한 헤더
using namespace std;
#define MAX_SIZE 80 //최대 80글자까지
string convert(string line);
int main(){
int count; //테스트케이스
int index = 0; //for문 안의 반복문을 처리할 변수
char *insert; //입력받을 문자열
string *result; //테스트케이스 수만큼 결과를 저장할 배열
string line; //입력받은 문자열을 연산할 변수
int length = 0; //입력받은 문자열의 길이를 저장할 변수
int find = 0; //입력받은 문자열에서 %의 위치를 찾아 저장할 변수
insert = new char[MAX_SIZE+1]; //null문자 포함
cin>>count;
result = new string[count]; //메모리 할당
cin.ignore();
for(int i=0;i<count;i++){
fgets(insert, MAX_SIZE+1, stdin);
line = insert;
line.pop_back(); //개행문자 삭제
length = line.length(); //입력받은 문자열의 길이를 계산
while(!line.empty()){ //모두 변환할 때 까지
find = line.find("%"); //23%32 일 경우 2 반환
//cout<<"find "<<find<<endl;
if(find>length||find<0){ //%가 문자열 안에 없을 때(문장 끝까지 변환을 완료했거나 유효하지 않은 위치에 존재할 때)
result[i].append(line); //문장 그대로 저장
break;
}
else{ //문자열 안에 %가 있을 때
result[i].append(line.substr(0, find)); //%앞부분을 입력
result[i].append(convert(line)); //유효한 uri을 변환하여 뒤에 입력(유효하지 않은 코드라면 그냥 입력)
//cout<<"convert "<<convert(line)<<endl;
//cout<<"result "<<result[i]<<endl;
line.erase(0, find+3); //변환 완료 부분 삭제
//cout<<"line "<<line<<endl;
}
}
}
for(int i=0;i<count;i++){
cout<<result[i]<<endl;
}
return 0;
}
string convert(string line){
string line_uri; //line 중 uri코드 부분
string unit; //변환된 기호
int comp = 0; //아스키코드랑 비교하기 위한 변수
//%뒤의 수를 기호로 변환
line_uri = line.substr(line.find("%")+1, 2); //%뒤의 기호를 나타내는 부분을 저장
comp = strtol(line_uri.c_str(), NULL, 16); //uri코드를 아스키코드로 변환
if((comp>31&&comp<48)||(comp>57&&comp<65)||(comp>90&&comp<97)||(comp>122&&comp<127)){
//cout<<"쏘옥"<<endl;
unit = comp; //기호로 변환
return unit; //변환한 기호 반환
}
return "%"+line_uri; //기호가 아니면 원래 값 반환
}