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
7
Solution.
The given code 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
Math,
16 days ago
Environmental Sciences,
16 days ago
India Languages,
1 month ago
Computer Science,
1 month ago
CBSE BOARD X,
8 months ago
Math,
8 months ago