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

4 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