Java: Find the length of last word of a specified string
Length of Last Word
Write a Java program to find the length of the last word in a given string. The string contains upper/lower-case alphabets and empty space characters like ' '.
Visual Presentation:
Sample Solution:
Java Code:
// Importing necessary Java utilities
import java.util.*;
// Main class Solution
public class Solution {
// Main method
public static void main(String[] args) {
// Initializing a string
String str1 = "The length of last word";
// Printing the original string
System.out.println("Original String: " + str1);
// Printing the length of the last word of the string
System.out.println("Length of the last word of the above string: " + length_Of_last_word(str1));
}
// Method to calculate the length of the last word in a string
public static int length_Of_last_word(String str1) {
int length_word = 0; // Initializing the variable to store the length of the last word
String[] words = str1.split(" "); // Splitting the string into words based on spaces
// Checking if words exist in the array after splitting
if (words.length > 0) {
// Assigning the length of the last word to the variable
length_word = words[words.length - 1].length();
} else {
length_word = 0; // If no words are present, setting the length to 0
}
return length_word; // Returning the length of the last word
}
}
Sample Output:
Original String: The length of last word Length of the last word of the above string: 4
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to determine the length of the first word in a given string.
- Write a Java program to find the longest word in a string and return its length.
- Write a Java program to count the total number of words in a given string.
- Write a Java program to trim extra spaces from a string and then compute the length of the last word.
Go to:
PREV : Swap Adjacent Nodes in List.
NEXT :Check Identical Binary Trees.
Java Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.