Advertisements
Advertisements
Question
Write a program in Java to accept a String from the user. Pass the String to a method First(String str) which displays the first character of each word.
Sample Input: Understanding Computer Applications
Sample Output: U
C
A
Code Writing
Solution
import java.util.*;
public class FQ13{
static void First(String s)
{s = ' ' + s; // add space in the beginning
for (int i = 0; i < s.length(); i++)
{if (s.charAt(i) = "") // if ch == space
System.out.println(s.charAt(i + 1));
}
}
public static void main(String args[])
{Scanner sn = new Scanner(System.in);
System.out.println("Enter a string");
First(sn.nextLine()); // Pass the Input as the argument to the method
}}
shaalaa.com
Is there an error in this question or solution?
Chapter 5: User - Defined Methods - EXERCISES [Page 339]