0

Given two strings(second string with single character) I want to remove all the occurrences of the second string in the first string, I tried executing the below code but it does not stop printing the string after the null character('/0'), instead it takes 0 from the null character and adds it to the string

int j=0;
    for(int i=0;i<s.length();i++){
        if(s[i]!=c[0]){
            s[j]=s[i];
            j++;
        }
        if(i==s.length()-1){
            s[j]='/0';
        }

I get an output like this :
string 1=hello
string 2=h
output which I get = ello0


    
  • `'\0'` means one thing, `'/0'` means something else. – Retired Ninja Sep 02 '22 at 03:21
  • `'/0'` is not what you think it is. You probably wanted `'\0'`. You shouldn't be terminating a `std::string` like this. It's not the correct way to do it. The string object maintains its length independently and you should use the `std::string::resize` method instead. Your entire algorithm could be replaced with the erase-remove idiom. See https://en.cppreference.com/w/cpp/algorithm/remove .. However, a quick fix is get rid of that second `if` in your loop and just do `s.resize(j);` after the loop. – paddy Sep 02 '22 at 03:21
  • @paddy thanks a lot it certainly worked, but why does not '\0' terminate the string? as far as i know it is used for termination of strings. can u please explain this –  Sep 02 '22 at 03:48
  • It technically does, but you are modifying the string object's data. The object will be keeping track of the string's length. Since C++11 the standard specifies that the `size()` and `length()` members are constant time. And it is unspecified in earlier versions of the standard (see [reference](https://en.cppreference.com/w/cpp/string/basic_string/size)). That means there's no guarantee that later on your personally-terminated string "ello" will report a length of 4. And in C++11 or later, it will definitely report 5. The null terminator and any characters that follow are part of the string. – paddy Sep 02 '22 at 10:48
  • In addition to that, let's imagine you removed _no_ characters from the string. In that case the non-const operator `s[j]` is actually an out-of-bounds access (prior to C++11) as far as the `std::string` interface is concerned. See [reference](https://en.cppreference.com/w/cpp/string/basic_string/operator_at). The standard has since cleared this up and specified that you _can_ modify this value provided you set it to the NUL character. – paddy Sep 02 '22 at 10:53
  • All this is to say: yes, ordinary strings (e.g. string literals) in C and C++ are terminated with a NUL character, but `std::string` is a class with its own rules. You must use the class as it's intended and avoid hacking around its data as if you're writing C. – paddy Sep 02 '22 at 10:55
  • Here is a [demo](https://godbolt.org/z/TcG5f8Eve) showing why it's bad to manually-terminate a `std::string`. – paddy Sep 02 '22 at 11:02
  • @paddy thanks a lot for clarifying. means a lot –  Sep 02 '22 at 16:24

0 Answers0