zoukankan      html  css  js  c++  java
  • <OFFER05> 05_ReplaceSpaces替换空格

    /*******************************************************************
    Copyright(c) 2016, Harry He
    All rights reserved.
    
    Distributed under the BSD license.
    (See accompanying file LICENSE.txt at
    https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
    *******************************************************************/
    
    //==================================================================
    // 《剑指Offer——名企面试官精讲典型编程题》代码
    // 作者:何海涛
    //==================================================================
    
    // 面试题5:替换空格
    // 题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”,
    // 则输出“We%20are%20happy.”。
    
    #include <cstdio>
    #include <cstring>
    #if 0
    
    /*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/
    void ReplaceBlank(char str[], int length)
    {
        if (str == nullptr && length <= 0)
            return;
    
        /*originalLength 为字符串str的实际长度*/
        int originalLength = 0;
        int numberOfBlank = 0;
        int i = 0;
        while (str[i] != '')
        {
            ++originalLength;
    
            if (str[i] == ' ')
                ++numberOfBlank;
    
            ++i;
        }
    
        /*newLength 为把空格替换成'%20'之后的长度*/
        int newLength = originalLength + numberOfBlank * 2;
        if (newLength > length)
            return;
    
        int indexOfOriginal = originalLength;
        int indexOfNew = newLength;
        while (indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
        {
            if (str[indexOfOriginal] == ' ')
            {
                str[indexOfNew--] = '0';
                str[indexOfNew--] = '2';
                str[indexOfNew--] = '%';
            }
            else
            {
                str[indexOfNew--] = str[indexOfOriginal];
            }
    
            --indexOfOriginal;
        }
    }
    
    #endif
    
    
    #if 1
    /*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/
    void ReplaceBlank(char str[], int length)
    {
        if ((str == nullptr) || (length < 0))
            return;
    
        //在原来字符串上进行替换,length保证开够了空间
        int length_of_origin = 0;
        int length_of_blank = 0;
        int i = 0;
    
        //如何知道字符串的长度
        while (str[i] != '')
        {
            if (str[i] == ' ')
            {
                length_of_blank++;
            }
            length_of_origin++;
            i++;
        }
    
        int length_new = length_of_origin + 2 * length_of_blank;
    
        int p1 = length_of_origin;
        int p2 = length_new;
    
        while (p2 > p1)
        {
            if (str[p1] != ' ')
            {
                str[p2] = str[p1];
    
                p2--;
                p1--;
            }
            else
            {
                p1--;
                str[p2--] = '0';
                str[p2--] = '2';
                str[p2--] = '%';
            }
        }
    
    
        
    }
    #endif
    
    // ====================测试代码====================
    void Test(char* testName, char str[], int length, char expected[])
    {
        if (testName != nullptr)
            printf("%s begins: ", testName);
    
        ReplaceBlank(str, length);
    
        if (expected == nullptr && str == nullptr)
            printf("passed.
    ");
        else if (expected == nullptr && str != nullptr)
            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();
    
        return 0;
    }
  • 相关阅读:
    HTML基础
    JavaScript基础
    J2EE技术(六)——JSP
    2011.2—至今年度总结
    J2EE技术(二)——JDBC
    DRP项目总结(一)——项目简介
    CSS基础
    J2EE技术(五)——EJB
    J2EE技术(三)——JMS
    J2EE技术(四)——JavaMail
  • 原文地址:https://www.cnblogs.com/focus-z/p/9906679.html
Copyright © 2011-2022 走看看