0

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

Here's what I've tried:

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);
}

But the website that tests the code for the correct answer is giving me the following error:

CTest1.cpp: In function 'std::string replace(std::string)':

CTest1.cpp:9: error: ISO C++ forbids comparison between pointer and integer

Please note that the lines in the error don't necessarily correlate with the actual lines in the code.

Any suggestions?

Update

Solved it using the following code:

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;
}
0

2 Answers 2

3
string sentence;
if(sentence[0] == " ")

" " isn´t a single char, but a whole string. If you want a single blank, use ' '

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! That resolved that error. Any ideas why it says my return value is incorrect?
@inquisitor What does it say exactly?
It just tells me that replace does not return the correct value. It's not compiled using an IDE, just an application my school uses to check output.
@inquisitor It doesn't return the correct output because in the case of a non-empty string which does not have a blank as the first character, it returns the rest of the string after that character at each recursive call. So, in the case of "ABC" it's going to return "" since it removes one character each time it doesn't see a blank.
You want something more like return '*' + replace(sentence.substr(1)); when you find a space... see how replace then works on successively less of the sentence? As is, you're either misunderstanding what append does or how it might contribute to a solution.
0
  1. In if(sentence[0] == " "), sentence[0] is a character(which may be converted to its ascii value(integer) during comparison) and " " is an empty string or pointer to first character in an empty string. That is what is throwing the error.
  2. Instead do if(sentence[0] == ' '). Also I am not sure in C++, but in java == checks for equality of references. so take care of it as well.
  3. Also you need to append the result of replacing the rest of the string, not just the rest of the string. Write a recursive function for that

1 Comment

Your first two points are not (completely) correct. The code will do as intended in C++ (well, only for ASCII/Single-Byte UTF8, but you shouldn't be using string for unicode anyway), provided @dviantfan's correction is made as [] returns char on string, not string. The 3rd point is correct however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.