PROBLEM LINK: Contest Page | CodeChef
Author: Setter’s name
Tester: Tester’s name
Editorialist: Editorialist’s name
DIFFICULTY : EASY
Bob loves a game where he finds the number of letters in the last word of a sentence. Given a string S, you are required to find the answer that Bob wants so he can easily finish the game.
Iterate from the characters at the end of the given string and count the number of characters until a first space is found in the string.
Input the string and initialise a counter variable to zero. Remove any unnecessary spaces at the end of the string and iterate through the characters at the end of the given string while incrementing this counter variable until the character ‘ ‘ is encountered in the string. The iteration terminates when this character is found and the value of the counter variable at this instant is the required answer.
public static void main(String args)
Scanner sc= new Scanner(System.in);
String s= sc.nextLine();
int len = 0;
s = s.trim();
for (int i = s.length() - 1; i >=0; i --)
if (s.charAt(i) == ’ ')