Advertisements
Advertisements
प्रश्न
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
कोड लेखन
उत्तर
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
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?