Write a java program to covert last letter of the word in uppercase in the string

java program to covert last letter of the word in uppercase in the string. Write a program to read a sentence as a string and store only the last letter of each word of the sentence in capital letters separated by $. Print the final string.

The return type (string) should return the final string.

Input and Output Format:

Input consists of a string.

Output consists of a string (the final string).

Refer sample output for formatting specifications.

Smaple Input :

This is a cat

Sample Output :

S$S$A$T

java program to covert last letter of the word in uppercase in the string

package com.demo;
 
import java.util.*;
 
public class Main {
 
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String n = sc.nextLine();
System.out.println(display(n));
}
 
public static String display(String input) {
String str1 = null;
StringTokenizer st = new StringTokenizer(input, " ");
StringBuffer sb = new StringBuffer();
while (st.hasMoreTokens()) {
str1 = st.nextToken();
String str2 = str1.substring(str1.length() - 1);
String str3 = str2.toUpperCase();
sb.append(str3).append("$");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
 
}

Add a Comment

Your email address will not be published. Required fields are marked *

six + 19 =