C ++ string error

I have an error code: invalid conversion from 'const char *' to 'char'

std::string zipZap(const std::string& str){
    for (unsigned i = 0; i < str.length(); i++){
        if (str[i] == 'z'){
            if (str[i+2] == 'p'){
                str[i+1] = "";
            }
        }
    }
    return str;
}
+3
source share
3 answers

The string const cannot be changed. If you want to change the delete const line from it.

And also you assign the string char to the row index string[i+1] = ""

Instead, it should be string[i+1] = ' 'orstring[i+1] = '\0'

+3
source
const std::string& string

string is const, you cannot change it like string[i+1] = "";

+1
source

:

str[i+1] = "";

, str[i+1] char, , char*.

str[i+1] = '\0'; , . , , const std::string& str std::string& str .

+1

All Articles