1.12
字符反转:
Sample Input
3
olleh !dlrow
m’I morf .udh
I ekil .mca
Sample Output
hello world!
I’m from hdu.
I like acm.
好久没有用 STL ,于是想到了string 数组的使用 ,还有reverse(s.begin(),s.end()),
发现不行 , 在这个题里 没法控制 流 ;(得看看流的那部分了)。
方法1:用char[] 数组 。
方法2:用栈做(因为先进后出,和栈非常类似)// 拓宽思路
方法一:
#if 1
#include
using namespace std;
int main()
{
char s[1001], s1[1001];
int len, i, j, p;
int n ;
cin>>n;
getchar();
while(n--)
{
gets(s);
len=strlen(s);
for(i=0,j=0; i<len; i++)
{
if(s[i]!=' ')
s1[j++]=s[i];
else
{
for(p=j-1; p>=0; p--)
cout<<s1[p];
cout <<" ";
j=0;
}
}
for(p=j-1; p>=0; p--)
cout <<s1[p];
cout << endl;
}
}