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
using System ; class GFG { public virtual int lengthOfLastWord ( string a ) { int len = 0 ; string x = a . Trim ( ) ; for ( int i = 0 ; i < x . Length ; i ++ ) { if ( x [ i ] == ' ▁ ' ) { len = 0 ; } else { len ++ ; } } return len ; } public static void Main ( string [ ] args ) { string input = " Geeks ▁ For ▁ Geeks ▁ " ; GFG gfg = new GFG ( ) ; Console . WriteLine ( " The ▁ length ▁ of ▁ last ▁ word ▁ is ▁ " + gfg . lengthOfLastWord ( input ) ) ; } }