编写递归字符串函数
Writing a recursive string function
Write a recursive, string -valued function, replace, that accepts a string and returns a new string consisting of the original string with each blank replaced with an asterisk (*)
Replacing the blanks in a string involves:
Nothing if the string is empty
Otherwise:
If the first character is not a blank, simply concatenate it with the result of replacing the rest of the string
If the first character IS a blank, concatenate an * with the result of replacing the rest of the string
这是我尝试过的方法:
string replace(string sentence){
if(sentence.empty()) return sentence;
string newString;
if(sentence[0] == " ") {
newString.append('*' + sentence.substr(1, sentence.length()));
} else {
newString.append(sentence.substr(1, sentence.length()));
}
return replace(newString);
}
但是测试代码正确答案的网站给我以下错误:
CTest1.cpp: In function 'std::string replace(std::string)':
CTest1.cpp:9: error: ISO C++ forbids comparison between pointer and integer
请注意,错误中的行不一定与代码中的实际行相关。
有什么建议吗?
更新
使用以下代码解决了它:
string replace(string sentence){
if(sentence.empty()) return sentence;
string newString;
if(sentence[0] == ' ') {
newString.append("*" + replace(sentence.substr(1)));
} else {
newString.append(sentence[0] + replace(sentence.substr(1)));
}
return newString;
}
string sentence;
if(sentence[0] == " ")
" "
不是单个字符,而是整个字符串。如果你想要一个空白,使用 ' '
- 在if(sentence[0] == " ")中,sentence[0]是一个字符(在比较时可以转换成它的ascii值(整数))," "是一个空字符串或指针空字符串中的第一个字符。这就是抛出错误的原因。
- 改为执行 if(sentence[0] == ' ')。我也不确定在 C++ 中,但在 java == 中检查引用是否相等。所以也要好好照顾它。
- 您还需要附加替换字符串其余部分的结果,而不仅仅是字符串的其余部分。为此写一个递归函数
Write a recursive, string -valued function, replace, that accepts a string and returns a new string consisting of the original string with each blank replaced with an asterisk (*) Replacing the blanks in a string involves:
Nothing if the string is empty
Otherwise: If the first character is not a blank, simply concatenate it with the result of replacing the rest of the string
If the first character IS a blank, concatenate an * with the result of replacing the rest of the string
这是我尝试过的方法:
string replace(string sentence){
if(sentence.empty()) return sentence;
string newString;
if(sentence[0] == " ") {
newString.append('*' + sentence.substr(1, sentence.length()));
} else {
newString.append(sentence.substr(1, sentence.length()));
}
return replace(newString);
}
但是测试代码正确答案的网站给我以下错误:
CTest1.cpp: In function 'std::string replace(std::string)':
CTest1.cpp:9: error: ISO C++ forbids comparison between pointer and integer
请注意,错误中的行不一定与代码中的实际行相关。
有什么建议吗?
更新
使用以下代码解决了它:
string replace(string sentence){
if(sentence.empty()) return sentence;
string newString;
if(sentence[0] == ' ') {
newString.append("*" + replace(sentence.substr(1)));
} else {
newString.append(sentence[0] + replace(sentence.substr(1)));
}
return newString;
}
string sentence;
if(sentence[0] == " ")
" "
不是单个字符,而是整个字符串。如果你想要一个空白,使用 ' '
- 在if(sentence[0] == " ")中,sentence[0]是一个字符(在比较时可以转换成它的ascii值(整数))," "是一个空字符串或指针空字符串中的第一个字符。这就是抛出错误的原因。
- 改为执行 if(sentence[0] == ' ')。我也不确定在 C++ 中,但在 java == 中检查引用是否相等。所以也要好好照顾它。
- 您还需要附加替换字符串其余部分的结果,而不仅仅是字符串的其余部分。为此写一个递归函数