Answers for "how to ignore space in string in c++"

C++
1

how to ignore space in string in c++

#include <iostream>

std::string IgnoreWhitespace(std::string str)
{
    for(int i = 0; i < str.length(); i++)
    {
        if(str[i] == ' ') str.erase(i, 1);
    }

    return str;
}

int main() 
{
    std::string str = "W h i t e s p a c e";

    std::cout << IgnoreWhitespace(str); // Output = Whitespace

    return 0;
}
Posted by: Guest on August-27-2021

Code answers related to "how to ignore space in string in c++"

Browse Popular Code Answers by Language