KnowledgeBoat Logo

Computer Applications

Write a program in Java to enter a sentence. Frame a word by joining all the first characters of each word of the sentence. Display the word.
Sample Input: Vital Information Resource Under Seize
Sample Output: VIRUS

Java

Java String Handling

84 Likes

Answer

import java.util.Scanner;

public class KboatFrameWord
{
    public static void main(String args[]) {
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a sentence:");
       String str = in.nextLine();
       String word = "" + str.charAt(0);
       int len = str.length();
       
       for (int i = 0; i < len; i++) {
           char ch = str.charAt(i);
           if (ch == ' ')
               word += str.charAt(i + 1);
       }
       
       System.out.println(word);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program in Java to enter a sentence. Frame a word by joining all the first characters of each word of the sentence. Display the word. Sample Input: Vital Information Resource Under Seize Sample Output: VIRUS

Answered By

31 Likes


Related Questions