Giter Club home page Giter Club logo

Comments (2)

LJoiner avatar LJoiner commented on May 26, 2024 3

现在给出的实现如下:

/*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] != '\0')
    {
        ++ 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;
    }
}

我的考虑是 originalLength 的计算,在 strlen(string) 时,确实像上面的计算长度是没有错的,不用考虑字符数组最后的空字符 \0,在这里的话,考虑边界条件,个人觉得应该添加如下改动:

  1. 在计算 newLength 之前,将 originalLength 给多计算一个字符,算上最后的空字符 \0,其他的保持不变即可;
  2. 如果考虑和 strlen(string) 一样的语义,在计算 originalLength 时就用如上的代码计算长度即可,但在判断得到的 newLength 是否超出 length 时,需要
    if(newLength +1 > length)
        return;

这样保证替换之后的字符串的最后一位空字符是字符数组提供的,在测试时,传入的字符数组是 char str[100] = "We are happy.",其13-99索引号均填充为 \0

以上 1 和 2 两种方案选择一种即可。

还有,开头的判断:if(str == nullptr && length <= 0) 应该改为: if(str == nullptr || length <= 0)

from codinginterviewchinese2.

mhsszm avatar mhsszm commented on May 26, 2024

边界条件未处理到位的情况,当测试用例1,改为:
// 空格在句子中间
void Test1()
{
const int length = 13;

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

}
按作者的意思是要return ;的,可实际并没有,导致后面程序崩溃。1楼,2楼讲的很好。

from codinginterviewchinese2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.