Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ String Error

Tags:

c++

I got a code with an error: 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;
}
like image 758
user3326237 Avatar asked Mar 03 '26 04:03

user3326237


2 Answers

const string cant be modified. If you want to modify the string remove const from it.

And also you are assigning string to char index of string string[i+1] = ""

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

like image 64
shivakumar Avatar answered Mar 05 '26 17:03

shivakumar


const std::string& string

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

like image 27
Pranit Kothari Avatar answered Mar 05 '26 16:03

Pranit Kothari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!