हिंदी

Print the words in a new line each.

Advertisements
Advertisements

प्रश्न

Print the words in a new line each.

कोड लेखन
Advertisements

उत्तर

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 11: Programs Overall - String Handling [पृष्ठ २५३]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 11 Programs Overall
String Handling | Q 3. | पृष्ठ २५३
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×