剑指offer--替换空格

题目:


网络背景(简单了解)


分析:





书上给出的代码:

// ReplaceBlank.cpp : Defines the entry point for the console application.
//

// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛

#include "stdafx.h"
#include <cstring>
#include<string.h>

/*length 为字符数组string的总容量*/
void ReplaceBlank(char string1[], int length)
{
    if(string1 == NULL && length <= 0)
        return;

    /*originalLength 为字符串string的实际长度*/
    int originalLength = 0;
    int numberOfBlank = 0;
    int i = 0;
    while(string1[i] != '\0')
    {
        ++ originalLength;

        if(string1[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(string1[indexOfOriginal] == ' ')
        {
            string1[indexOfNew --] = '0';
            string1[indexOfNew --] = '2';
            string1[indexOfNew --] = '%';
        }
        else
        {
            string1[indexOfNew --] = string1[indexOfOriginal];
        }

        -- indexOfOriginal;
    }
}

void Test(char* testName, char string1[], int length, char expected[])
{
    if(testName != NULL)
        printf("%s begins: ", testName);

    ReplaceBlank(string1, length);

    if(expected == NULL && string1 == NULL)
        printf("passed.\n");
    else if(expected == NULL && string1 != NULL)
        printf("failed.\n");
    else if(strcmp(string1, expected) == 0)
        printf("passed.\n");
    else
        printf("failed.\n");
}

// 空格在句子中间
void Test1()
{
    const int length = 100;

    char string1[length] = "hello world";
    Test("Test1", string1, length, "hello%20world");
}

// 空格在句子开头
void Test2()
{
    const int length = 100;

    char string1[length] = " helloworld";
    Test("Test2", string1, length, "%20helloworld");
}

// 空格在句子末尾
void Test3()
{
    const int length = 100;

    char string1[length] = "helloworld ";
    Test("Test3", string1, length, "helloworld%20");
}

// 连续有两个空格
void Test4()
{
    const int length = 100;

    char string1[length] = "hello  world";
    Test("Test4", string1, length, "hello%20%20world");
}

// 传入NULL
void Test5()
{
    Test("Test5", NULL, 0, NULL);
}

// 传入内容为空的字符串
void Test6()
{
    const int length = 100;

    char string1[length] = "";
    Test("Test6", string1, length, "");
}

//传入内容为一个空格的字符串
void Test7()
{
    const int length = 100;

    char string1[length] = " ";
    Test("Test7", string1, length, "%20");
}

// 传入的字符串没有空格
void Test8()
{
    const int length = 100;

    char string1[length] = "helloworld";
    Test("Test8", string1, length, "helloworld");
}

// 传入的字符串全是空格
void Test9()
{
    const int length = 100;

    char string1[length] = "   ";
    Test("Test9", string1, length, "%20%20%20");
}

int _tmain(int argc, _TCHAR* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();
    Test8();
    Test9();

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值