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

将字符串student a am i反转为 i am a student

时间:2015-05-09 16:33:57      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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

void reserve_string(char *l, char *r)
{
    while(l < r)
    {
        char tmp = *l;
        *l = *r;
        *r = tmp;
        l ++;
        r --;
    }
}
void reserve(char ch[], int len)
{
    int i = 0;
    char *chl = ch;
    char *chr = ch;
    while(ch[i])  
    {
        if(ch[i] == ‘ ‘)
        {
            reserve_string(chl, chr - 1);  //先反转每个单词,函数的嵌套调用 tneduts a ma i
            chl = ++chr; //先给chr +1,再把ch的值付给chl

        }
        else
        {
            chr ++;
        }
        i++;
    }
    reserve_string(ch, ch +len-1);  //再整体反转  i am a student

}
int main()

{
    char ch[] = "student a am i";
    reserve(ch , strlen(ch));
    printf("%s\n",ch);
    return 0;
}

将字符串student a am i反转为 i am a student

标签:

原文地址:http://blog.csdn.net/yangrujing/article/details/45602077

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