Frame a Word using a Sentence in Java

Write a program in Java to input a sentence in uppercase and frame a word by adding the first letter of each word and also arrange the alphabets of the new word in an alphabetical order.

Example:
INPUT:
HELLO AND WELCOME!
OUTPUT:
The new word: HAW
Arranged in alphabetical order: AHW

import java.io.*;
import java.util.StringTokenizer;
class FrameWord{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Sentence: ");
        String s = br.readLine();
        s = s.trim();
        s = s.toUpperCase();
        String word = "";
        String asc = "";
        StringTokenizer st = new StringTokenizer(s, " !.?,");
        int count = st.countTokens();
        for(int i = 1; i <= count; i++)
            word += st.nextToken().charAt(0);
        System.out.println("The new word: " + word);
        char ch[] = new char[word.length()];
        for(int i = 0; i < ch.length; i++)
            ch[i] = word.charAt(i);
        for(int i = 0; i < ch.length; i++){
            for(int j = 0; j < ch.length - 1 - i; j++){
                if(ch[j] > ch[j + 1]){
                    char temp = ch[j];
                    ch[j] = ch[j + 1];
                    ch[j + 1] = temp;
                }
            }
        }
        for(int i = 0; i < ch.length; i++)
            asc += ch[i];
        System.out.println("Arranged in alphabetical order: " + asc);
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.