zoukankan      html  css  js  c++  java
  • 4--替换空格

    /*
    题目要求:
        替换空格。
        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] != ''; i++)
        {
            if (str[i] == ' ')
                count_space++;
            str_length++;
        }
    //    printf("%d
    ",count_space);
    
        int p1 = str_length;
        int p2 = str_length + count_space * 2;
        printf("%d, %d
    ", 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
    ", str);
        return 0;
    }
  • 相关阅读:
    GET 请求和 POST 请求
    爬虫
    模板继承
    静态文件配置
    终端cmd创建django
    商城商品分类导航效果
    css样式
    视图部分
    django初识和路由
    【源码分析】cocos2dx的Action
  • 原文地址:https://www.cnblogs.com/hgonlywj/p/4842543.html
Copyright © 2011-2022 走看看