-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
面试题5:替换空格 #16
Comments
还有,开头的判断:if(str == nullptr && length <= 0) 应该改为: if(str == nullptr || length <= 0) |
边界条件未处理到位的情况,当测试用例1,改为:
} |
现在给出的实现如下:
我的考虑是
originalLength
的计算,在strlen(string)
时,确实像上面的计算长度是没有错的,不用考虑字符数组最后的空字符\0
,在这里的话,考虑边界条件,个人觉得应该添加如下改动:newLength
之前,将originalLength
给多计算一个字符,算上最后的空字符\0
,其他的保持不变即可;strlen(string)
一样的语义,在计算originalLength
时就用如上的代码计算长度即可,但在判断得到的newLength
是否超出length
时,需要这样保证替换之后的字符串的最后一位空字符是字符数组提供的,在测试时,传入的字符数组是
char str[100] = "We are happy."
,其13-99索引号均填充为\0
以上 1 和 2 两种方案选择一种即可。
The text was updated successfully, but these errors were encountered: