| 5 | |
| 6 | |
| 7 | class Solution { |
| 8 | public: |
| 9 | std::string & ltrim(std::string & str) |
| 10 | { |
| 11 | auto it2 = std::find_if( str.begin() , str.end() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } ); |
| 12 | str.erase( str.begin() , it2); |
| 13 | return str; // returns the string with no extra blank spaces at the front. |
| 14 | } |
| 15 | |
| 16 | std::string & rtrim(std::string & str) |
| 17 | { |
| 18 | auto it1 = std::find_if( str.rbegin() , str.rend() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } ); |
| 19 | str.erase( it1.base() , str.end() ); |
| 20 | return str; // returns the string with no extra blank spaces at the back. |
| 21 | } |
| 22 | |
| 23 | int lengthOfLastWord(string s) { |
| 24 | s = ltrim(rtrim(s)); // removes extra spaces fromfront and back of string |
| 25 | string t = ""; |
| 26 | for(char c: s){ // iterate through the string to get all words and finally having last word in string t. |
| 27 | if(c == ' ') |
| 28 | t = ""; |
| 29 | else |
| 30 | t += c; |
| 31 | } |
| 32 | return t.size(); // returns the size of t which contains the last word. |
| 33 | } |
| 34 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected