首页 > 其他 > 详细

4--替换空格

时间:2015-09-27 19:58:59      阅读:192      评论:0      收藏:0      [点我收藏+]
/*
题目要求:
    替换空格。
    we are happy。
    we%20are%20happy。


算法解析:
    字符串长度为14.
    先计算有多少个空格,测试字符串为2个。这样总长度为18.
    从最后一个字符向后移动,注意控制指针,空格是1个字符,%20是三个。


*/


#include <stdio.h>

void replaceStr(char str[])
{
    int count_space = 0;
    int str_length = 0;
    for (int i = 0; str[i] != \0; i++)
    {
        if (str[i] ==  )
            count_space++;
        str_length++;
    }
//    printf("%d\n",count_space);

    int p1 = str_length;
    int p2 = str_length + count_space * 2;
    printf("%d, %d\n", p1, p2);

    while (p1 != p2 || p1 < 0)
    {
        while (str[p1] !=  )
        {
            str[p2--] = str[p1--];
        }

        str[p2--] = 0;
        str[p2--] = 2;
        str[p2--] = %;

        p1--;
    }

}

int main()
{
    char str[30] = "we are happy.";
    replaceStr(str);

    printf("%s\n", str);
    return 0;
}

 

4--替换空格

原文:http://www.cnblogs.com/hgonlywj/p/4842543.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!