zoukankan      html  css  js  c++  java
  • 《剑指offer》第五题(替换空格)

    // 替换空格
    // 题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”,
    // 则输出“We%20are%20happy.”。
    
    #include <iostream>
    using namespace std;
    
    bool replace_space(char* str,const int length)
    {
        if (str == NULL && length <= 0)//上来先判断
            return false;
    
        int true_length = 0, count = 0, i = 0;
        while (str[i] != '')//这里要用单引号
        {
            ++true_length;//输入的length是数组最大长度,真实长度有待检测
            if (str[i] == ' ')
                ++count;
            ++i;
        }
    
        int new_length = true_length + 2 * count;//算法核心思想,从尾到头复制
        if (new_length > length)//要判断原数组长度够长不
            return false;//难受,写的时候落下个分号
    
        for (int i = count; i > 0;)//当所有空格被替代完后结束
        {
            if (str[true_length] == ' ')
            {
                new_length=new_length - 2;//如果该复制空格了,先提前移两位位,并赋值“%20”,注意是两位!!
                str[new_length] = '%';//这里也要用单引号
                str[new_length+1] = '2';
                str[new_length+2] = '0';//这里也写错过,是+2,不小心写成+1!!
                i--;//这个判断条件也写错过,刚才写进for()中了!!
            }
            else
                str[new_length] = str[true_length];
    
            new_length -= 1;
            true_length -= 1;
    
        }
        return true;
    }
    
    // ====================测试代码====================
    void Test(const char* testName, char str[], int length, const char expected[])
    {
        if (testName != NULL)
            printf("%s begins: ", testName);
    
        replace_space(str, length);
    
        if (expected == NULL && str == NULL)
            printf("passed.
    ");
        else if (expected == NULL && str != NULL)
            printf("failed.
    ");
        else if (strcmp(str, expected) == 0)
            printf("passed.
    ");
        else
            printf("failed.
    ");
    }
    
    // 空格在句子中间
    void Test1()
    {
        const int length = 100;
    
        char str[length] = "hello world";
        Test("Test1", str, length, "hello%20world");
    }
    
    // 空格在句子开头
    void Test2()
    {
        const int length = 100;
    
        char str[length] = " helloworld";
        Test("Test2", str, length, "%20helloworld");
    }
    
    // 空格在句子末尾
    void Test3()
    {
        const int length = 100;
    
        char str[length] = "helloworld ";
        Test("Test3", str, length, "helloworld%20");
    }
    
    // 连续有两个空格
    void Test4()
    {
        const int length = 100;
    
        char str[length] = "hello  world";
        Test("Test4", str, length, "hello%20%20world");
    }
    
    // 传入nullptr
    void Test5()
    {
        Test("Test5", nullptr, 0, nullptr);
    }
    
    // 传入内容为空的字符串
    void Test6()
    {
        const int length = 100;
    
        char str[length] = "";
        Test("Test6", str, length, "");
    }
    
    //传入内容为一个空格的字符串
    void Test7()
    {
        const int length = 100;
    
        char str[length] = " ";
        Test("Test7", str, length, "%20");
    }
    
    // 传入的字符串没有空格
    void Test8()
    {
        const int length = 100;
    
        char str[length] = "helloworld";
        Test("Test8", str, length, "helloworld");
    }
    
    // 传入的字符串全是空格
    void Test9()
    {
        const int length = 100;
    
        char str[length] = "   ";
        Test("Test9", str, length, "%20%20%20");
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();//是无效输入
        Test6();//是空输入,这俩还不太一样
        Test7();
        Test8();
        Test9();
    
        system("pause");
    }

     

  • 相关阅读:
    Http中GET和POST两种请求的区别
    JSON学习笔记
    分页
    python 函数,闭包
    LVS负载均衡中arp_ignore和arp_annonuce参数配置的含义
    return ;
    openssl 在php里
    重装drupal
    protected的意义
    和 和 notepad++
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/10462555.html
Copyright © 2011-2022 走看看