Answers for "calculates the length of last word"

0

calculates the length of last word

// Java program for implementation of simple
// approach to find length of last word
public class GFG {
    public int lengthOfLastWord(final String a)
    {
        int len = 0;
 
        /* String a is 'final'-- can not be modified
           So, create a copy and trim the spaces from
           both sides */
        String x = a.trim();
 
        for (int i = 0; i < x.length(); i++) {
            if (x.charAt(i) == ' ')
                len = 0;
            else
                len++;
        }
        return len;
    }
    // Driver code
    public static void main(String[] args)
    {
        String input = "Geeks For Geeks  ";
        GFG gfg = new GFG();
        System.out.println("The length of last word is " + gfg.lengthOfLastWord(input));
    }
}
Posted by: Guest on March-16-2022

Code answers related to "calculates the length of last word"

Browse Popular Code Answers by Language