English

Print the words in a new line each.

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?
Chapter 11: Programs Overall - String Handling [Page 253]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 11 Programs Overall
String Handling | Q 3. | Page 253
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×