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.
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程序设计 |
Given a valid identifier in C programs, please write a program to reverse it by respectively reversing two parts separated by ‘_’.
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 ‘_’.
For each test case, print out the string reversed.
3 John_Smith int_45 _me
nhoJ_htimS tni_54 _em
#include<iostream>
#include<string>
using namespace std;
int main() {
int num,j;
string stack;
cin>>num;
for(int i=0; i<num; i++) {
cin>>stack;
int k=0,o=0;
for(; k<stack.length(); k++) {
if(stack[k]=='_') {
o=1;
for(j=k-1; j>=0; j--)
cout<<stack[j];
cout<<"_";
for(int l=stack.length()-1; l>k; l--)
cout<<stack[l];
cout<<endl;
}
}
if(o==0) {
for(int u=k-1; u>=0; u--)
cout<<stack[u];
cout<<endl;
}
}
}
|