简体   繁体   中英

Why is my if function not erasing blank space from a string? Can it not detect blank space inputs in c++? Is there a method to remove space from input

Question - Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters from the set in one line, separated by a comma. He also added an opening curved bracket at the beginning of the line and a closing curved bracket at the end of the line.

Unfortunately, from time to time Anton would forget writing some letter and write it again. He asks you to count the total number of distinct letters in his set.

Input - The first and the single line contains the set of letters. The length of the line doesn't exceed 1000. It is guaranteed that the line starts from an opening curved bracket and ends with a closing curved bracket. Between them, small English letters are listed, separated by a comma. Each comma is followed by a space.

Output - Print a single number — the number of distinct letters in Anton's set.

Code Forces question link - https://codeforces.com/problemset/problem/443/A

My code -

int main(){
    string s;
    int count=0;
    getline(cin,s);
    for(int i=0;i<s.length();i++){
        if(s[i]==' ' || s[i]=='}' || s[i]=='{' || s[i]==','){
            s.erase(i,1);
        }
    }
    for(int i=0;i<s.length();i++){
        cout << s[i];
    }
    sort(s.begin(),s.end());
    for(int i=0;i<s.length();i++){
        if(s[i]==s[i+1]){
            count++;
        }
    }
    cout << endl << (s.length()-count);
}

The problem is that when I am outputting the string, it also show the blank spaces which I don't want it to. The input will contain spaces, for example - {a, b, c}. The if function is somehow not able to erase that blank space from the string, what could be the problem and how do I solve it?

As already found, the issue is that after erasing one character, i is again incremented, skipping over the next one. After erasing you should not increment i .

for(int i=0;i<s.length();){
    if(s[i]==' ' || s[i]=='}' || s[i]=='{' || s[i]==','){
        s.erase(i,1);
    } else {
        i++;
    }
}

But also the solution with individual erase() calls is O(N 2 ) because each time erase has to move all remaining characters forward.

Consider using the erase-remove idiom instead for O(N) performance:

s.erase(std::remove_if(s.begin(), s.end(), [](char c) {
    return c == ' ' || c == '}' || c == '{' || c == ',';
}), s.end());

The index of the next character after it is erased will remain i

Updated code -

int main(){
    string s;
    int count=0;
    getline(cin,s);
    for(int i=0;i<s.length();i++){
        if(s[i]==' ' || s[i]=='}' || s[i]=='{' || s[i]==','){
            s.erase(i,1);
            i--;
        }
    }
    for(int i=0;i<s.length();i++){
        cout << s[i];
    }
    sort(s.begin(),s.end());
    for(int i=0;i<s.length();i++){
        if(s[i]==s[i+1]){
            count++;
        }
    }
    cout << endl << (s.length()-count);
}

The C++ way to do this:

  for(int i=0;i<s.length();i++){
        if(s[i]==' ' || s[i]=='}' || s[i]=='{' || s[i]==','){
            s.erase(i,1);
            i--;
        }

would be:

 const std::string get_rid_of(" ,{}");
 s.erase(std::remove_if(s.begin(), s.end(), [=](char c)
         {
             return get_rid_of.find(c) != std::string::npos;
         }), s.end());

You're increasing i even when you have erased a character which makes it skip one character. As others have pointed out, only increase i when you don't erase .

Since C++20 can use the std::erase_if(std::basic_string) function instead:

std::erase_if(
    s,
    [](char c) { return /* your condition */ ; }
);

The type of iterator you chose is invalid, it will just erase the whole string.
Try this instead:

string::iterator itr;
itr = s.begin(); 
s.erase(itr); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM