Length Of Last Word in a String | C ++ program for implementation of simple approach to find length of last word ; String a is ' final ' -- can not be modified So , create a copy and trim the spaces from both sides ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <boost/algorithm/string.hpp> NEW_LINE using namespace std ; int lengthOfLastWord ( string a ) { int len = 0 ; string str ( a ) ; boost :: trim_right ( str ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . at ( i ) == ' ▁ ' ) len = 0 ; else len ++ ; } return len ; } int main ( ) { string input = " Geeks ▁ For ▁ Geeks ▁ " ; cout << " The ▁ length ▁ of ▁ last ▁ word ▁ is ▁ " << lengthOfLastWord ( input ) ; }