Computer Applications
Write a Java program to enter any sentence and convert the sentence to uppercase. Print only those words of the sentence whose first and last letters are the same.
Java
Java String Handling
5 Likes
Answer
import java.util.Scanner;
public class KboatString
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String str = in.nextLine();
str = str.toUpperCase();
str += " ";
int len = str.length();
String word = "";
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
char first = word.charAt(0);
char last = word.charAt(word.length() - 1);
if (first == last) {
System.out.println(word);
}
word = "";
}
else {
word += ch;
}
}
}
}Output

Answered By
1 Like
Related Questions
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024Define a class to accept the gmail id and check for its validity.
A gmail id is valid only if it has:
→ @
→ .(dot)
→ gmail
→ com
Example:
icse2024@gmail.comis a valid gmail id