Computer Science, asked by sreecsbhagya, 1 month ago

Write a program to input a string by using scanner class. Display the new string which is formed by the first character of each string.

Sample Input: Automated Teller Machine
Sample Output: ATM​

Answers

Answered by anindyaadhikari13
7

Solution.

The given co‎‎de is written in Java.

import java.util.*;

public class StringOperation{

   public static void main(String args[]){

       System.out.print("Enter a string: ");

       String s=(new Scanner(System.in)).nextLine();

       String arr[]=s.split(" ");

       String newString="";

       for(String i:arr)

           newString+=i.charAt(0);

       System.out.println("New String: "+newString);

   }

}

Explanation.

  • Here, we have asked the user to input a string.
  • Then, we have split the string into words and stored them in an array.
  • Then, we have concatenated the first letter of each word and stored the new string in another variable.
  • The variable is now displayed on the screen.

See attachment for output.

Attachments:
Similar questions