Length of last word of sentence

Write a program to find the length of last word.

public class MyClass {
    public static void main(String args[]) {
        
        String str = "rose is red sky is blue";
        int count = 0;
        for(int i=str.length()-1; i>=0; i--){
            char ch = str.charAt(i);
            if(ch==' '){
                break;
            }
            count++;
        }
        System.out.println(count);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *