码迷,mamicode.com
首页 > 其他好文 > 详细

将字符串反转,但单词不倒置。Right here waiting for you! -> you! for waiting here Right

时间:2014-07-13 09:31:51      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   io   div   

 

//将字符串反转,但单词不倒置。

#include<stdio.h>
#include<string.h>

void reverse(char *s)
{
    char data[255][255];//将s中的空格和非空格子串进行存储
    int row = 0,column = 0;
    int i,j,k;

    for(i=0;s[i];)
    {
        data[row][column] = s[i];
        column++;

        //找到了空格子串的开头
        if(s[i] ==  )
        {
            i++;
            if(s[i] ==  )
            {
                do
                {
                    data[row][column++] = s[i++];
                }while(s[i] ==  );

                data[row++][column] = \0;
                column = 0;
            }
            else
            {
                data[row++][column] = \0;
                column = 0;
            }
        }

        //找到了非空格子串的开头
        else if(s[i] !=  )
        {
            i++;
            if(s[i] !=  )
            {
                do
                {
                    data[row][column++] = s[i++];
                }while(s[i] !=   && s[i]);//注意非空格字符还包括字符串结束符。

                data[row++][column] = \0;
                column = 0;
            }
            else
            {
                data[row++][column] = \0;
                column = 0;
            }
        }
    }

    //将data[][]里面的子串存放于s中。
    k = 0;
    for(i=row-1;i>=0;i--)
    {
        for(j=0;data[i][j]!=\0;j++)
        {
            s[k++] = data[i][j];
        }
    }
    s[k] = \0;
}

int main()
{
    char s[255];
    gets(s);
    reverse(s);
    puts(s);
}

将字符串反转,但单词不倒置。Right here waiting for you! -> you! for waiting here Right,布布扣,bubuko.com

将字符串反转,但单词不倒置。Right here waiting for you! -> you! for waiting here Right

标签:style   blog   color   for   io   div   

原文地址:http://www.cnblogs.com/Camilo/p/3838118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!