前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >字符串反转-HDU 1062

字符串反转-HDU 1062

作者头像
ACM算法日常
发布2018-08-07 19:40:15
5400
发布2018-08-07 19:40:15
举报
文章被收录于专栏:ACM算法日常

Problem Description

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains a single line with several words. There will be at most 1000 characters in a line.

Output

For each test case, you should output the text which is processed.

Sample Input

代码语言:javascript
复制
3
olleh !dlrow
m'I morf .udh
I ekil .mca

Sample Output

代码语言:javascript
复制
hello world!
I'm from hdu.
I like acm.

字符串反转源码:

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>

int main()
{
    int n;
    /*scanf返回成功读取的个数,或者EOF,EOF=-1
    负数的取反操作等于正数取反加1再取反
    -1 -> 0001 -> 1110 + 1 -> 1111 -> 0000*/
    while (~scanf("%d", &n)) {
        /*scanf读取n后,会留下一个\n在缓冲区,通过
        getchar进行消化,不然下面的gets函数会读取\n*/
        getchar();
        while (n--) {
            char s[1010] = "";
            gets(s);
            int i, j;
            int flag = 0;
            /*从0开始处理字符串*/
            for (i = 0; i < (int)strlen(s); ++i) {
                /*碰到空格*/
                if (s[i] == ' ') {
                    /*倒着开始读取每一个字符*/
                    for (j = i - 1; j >= flag; j--) {
                        printf("%c", s[j]);
                    }
                    printf(" ");
                    /*标记位置*/
                    flag = i + 1;
                }
            }
            /*上面i已经遍历到了最后,直接倒着读取字符输出*/
            for (j = i - 1; j >= flag; j--) {
                printf("%c", s[j]);
            }
            printf("\n");
        }
    }

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-12-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档