Advertisements
Advertisements
Question
Print the words in a new line each.
Code Writing
Advertisements
Solution
import java.util.*;
class clString
{
public void main()
{
Scanner sc = new Scanner(System.in);
String ts = "", word = "";
System.out.print("Enter the string : ");
ts = sc.nextLine();
ts = ts + " ";
for(int j = 0; j <= ts.length() - 1; j++)
{
char ch = ts.charAt(j);
if(ch == ' ')
{
System.out.println(word);
word = "";
}
else
{
word = word + ch;
}
}
}
}
Output:
Enter the string : ROYAL BENGAL TIGER
ROYAL
BENGAL
TIGER
shaalaa.com
Is there an error in this question or solution?
