0
How do you remove characters from string?
For eg user enters "Hello world " I want to delete "Hello " And output back "world" No matter how long the string is "This is sololearn" Output back "is sololearn"
5 Answers
+ 1
#include <string>
#include <iostream>
int main()
{
using std::string, std::cout;
string s1 {"Hello world"}, s2 {"This is SoloLearn"};
// find position of last space
string::size_type pos {s1.find_last_of( ' ' )};
// if <pos> is valid
if( pos != string::npos )
cout << s1.substr( pos + 1 ) << "\n";
pos = s2.find_last_of( ' ' );
if( pos != string::npos )
cout << s2.substr( pos + 1 ) << "\n";
return 0;
}
/*
References
https://en.cppreference.com/w/cpp/string/basic_string/npos
https://en.cppreference.com/w/cpp/string/basic_string/find_last_of
https://en.cppreference.com/w/cpp/string/basic_string/substr
*/
+ 1
ASJADđč
1. The remove() function is a part pf the <algorithm> header file. You should specify that.
2. erase() is not a function, but a method on the std::string type. You should specify that too.
Sheharyar Nadeem
references to
remove() function in <algorithm>: https://en.cppreference.com/w/cpp/algorithm/remove
erase() method on string: https://en.cppreference.com/w/cpp/string/basic_string/erase
0
Print the next characters when space is encountered