English

Write a program to accept a sentence. Display the sentence in reversing order of its word. Sample Input: Computer is Fun Sample Output: Fun is Computer - Computer Applications

Advertisements
Advertisements

Question

Write a program to accept a sentence. Display the sentence in reversing order of its word.

Sample Input: Computer is Fun
Sample Output: Fun is Computer

Code Writing

Solution

import java.util.*;
public class SQ10
{public static void main(String args[])
{String s, wrd = "", text = "";
Scanner sn = new Scanner(System.in);
System.out.println("Enter the sentence to be reversed");
s = sn.nextLine() + " "; // add trailing space

for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) != ' ')
wrd = wrd + s.charAt(i);
else
{text = wrd + " " + text;
wrd = "";
}}

System.out.println(text);
}}
shaalaa.com
  Is there an error in this question or solution?
Chapter 4: String Handling - EXERCISES [Page 290]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 4 String Handling
EXERCISES | Q IX. 10. | Page 290
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×