https://www.codechef.com/MCL12021/problems/MCLP1105

PROBLEM LINK: CodeChef: Practical coding for everyone

Author: Setter’s name

Tester: Tester’s name

Editorialist: Editorialist’s name

DIFFICULTY : EASY

PREREQUISITES:

Nill

PROBLEM:

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.

QUICK EXPLANATION:

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.

EXPLANATION:

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.

SOLUTIONS:

Setter's Solution

import java.util.*;

class Main

{

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) == ’ ')

{

break;

}

else

{

len ++;

}

}

System.out.println(len);

}

}

Tester's Solution

Same Person

Editorialist's Solution

Same Person