DEV Community

Vasanth S
Vasanth S

Posted on

1

Practice Program in java Day-10

Today I learned the new topic of String:
find the length of last word in a string;

program :
class Solution {
public int lengthOfLastWord(String s) {
int last = s.length() - 1;

    while (last >= 0 && s.charAt(last) == ' ') {//to remove he spaces
        last--;
    }

    int start = last;
    while (start >= 0 && s.charAt(start) != ' ') {
        start--;
    }

    return last - start;        
}
public static void main(String... args){
    String a="fly of moon ";
    int S=lengthOfLastWlrd(a);
    System.out.println(S);}
Enter fullscreen mode Exit fullscreen mode

}
}

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay