How should i implement a string that will return all the contents of it after removing all of its whitespaces. For example, if my string is "aa bb ab", then it should return "aabbab" after removing the whitespaces. What should be the algorithm be ?
How should i implement a string that will return all the contents of it after removing all of its whitespaces. For example, if my string is "aa bb ab", then it should return "aabbab" after removing the whitespaces. What should be the algorithm be ?
you can use two loops, one will traverse the char array 1 char at a time and the other will check for spaces and skip over them. This method is best cos it doesnt require a second array to copy the data to.
Have a try and if you cant get it I'll give a little more help.
Code:#include <string> #include <algorithm> #include <cctype> using namespace std; string remove_whitespace(string s) { s.erase(remove_if(s.begin(), s.end(), &isspace), s.end()); return s; }
- lmov