Time Limit: 1 secs, Memory Limit: 256 MB
Description
Given a valid identifier in C programs, please write a program to reverse it by respectively reversing two parts separated by ‘_’.
Input
The first line is an integer m, indicating the number of test cases.
Then there are m lines and each line contains a string. A string will contain no more than 100 characters and there is one or less ‘_’.
Output
For each test case, print out the string reversed.
Sample Input
3
John_Smith
int_45
_me
Sample Output
nhoJ_htimS
tni_54
_em
Problem Source
10级CS程序设计
╥﹏╥… Just do it!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int T, index;
string str;
cin >> T;
while (T--)
{
cin >> str;
index = 0;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '_'){
index = i - 1;
for (int j = index; j >= 0; j--)
cout << str[j];
cout << '_';
index = i + 1;
for (int j = str.size() - 1; j >= index; j--)
cout << str[j];
break;
}
}
if (index == 0)
for (int i = str.size() - 1; i >= 0; i--)
cout << str[i];
cout << endl;
}
}