1.2.3text reverse

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        stack<char> s;
        while(true)
        {
            char ch=getchar();
            if(ch==' '||ch=='\n'||ch==EOF)
            {
                while(!s.empty())
                {
                    printf("%c",s.top());
                    s.pop();
                }
                if(ch=='\n'||ch==EOF)
                {
                    printf("\n");
                    break;
                }
                else printf(" ");
            }
            else
                s.push(ch);
        }
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;

int main()
{
    char s1[1005],s2[105];
    int n,l;
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        gets(s1);
        l=strlen(s1);
        for(int i=0,j=0,t=0;i<l;i++)
        {
            if(s1[i]!=' ')
                s2[j++]=s1[i];
            else
            {
                if(t)printf(" ");
                for(j=j-1;j>=0;j--)
                    printf("%c",s2[j]);
                t=1;
                j=0;
            }
            if(i==l-1)
            {
                if(t)printf(" ");
                for(j=j-1;j>=0;j--)
                    printf("%c",s2[j]);
                printf("\n");
            }
        }
    }
    return 0;
}

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=2&problemid=5

Sample Input
3
olleh !dlrow
m'I morf .udh
I ekil .mca
 
Sample Output
hello world!
I'm from hdu.
I like acm.
posted @ 2017-03-02 09:48  哲贤  阅读(168)  评论(0编辑  收藏  举报