Practice Program in java Day-10
Vasanth S

Vasanth S @vasanthvnr_31

About: I am Vasanth from the department of Information Technology, currently pursing my UG degree. I have been learning the flutter app development and also a Java programing language.

Joined:
Jan 27, 2025

Practice Program in java Day-10

Publish Date: Feb 19
1 0

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

}
}

Comments 0 total

    Add comment