Problem Statement
Write a Java program to print the last character of every word in a given string
Conditions
Ignore all the digits and whitespaces
Input
Hey3 Java Learners
Output
as
Explanation
As the first word Hey3 has digit in its last position, ignore it. The second word Java has multiple white spaces after it. But those white spaces should be ignored and only the last character should be printed. Likewise, the last character of the word Learners should be printed. Thus, the output as.
code:
import java.util.Scanner;
class Solution{
public static void main(String[] args){
Scanner s=new Scanner(System.in);
String input=s.nextLine();
s.close();
char[] result = input.toCharArray();
for(int i=0;i<result.length;i++){
if(Character.isWhitespace(result[i]) && !Character.isDigit(result[i-1]) && !Character.isWhitespace(result[i-1])){
System.out.print(result[i-1]);
}
else if(i == result.length-1 && !Character.isDigit(result[i])){
System.out.print(result[i]);
}
}
}
}
Comments
Post a Comment