How do you remove characters from string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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"

15th Jan 2021, 8:03 AM
Sheharyar Nadeem
Sheharyar Nadeem - avatar
3 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 */
15th Jan 2021, 9:03 AM
Ipang
+ 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
15th Jan 2021, 9:13 AM
XXX
XXX - avatar
0
Print the next characters when space is encountered
15th Jan 2021, 8:17 AM
Atul [Inactive]