WAP to accept a sentence.Display the sentence in reversing order of its word
Answers
Answer:
import java.util.Scanner;
public class ReverseStringWordByWordProgram
{
public static String reverseTheSentence(String inputString)
{
String[] words = inputString.split("\s");
String outputString = "";
for (int i = words.length-1; i >= 0; i--)
{
outputString = outputString + words[i] + " ";
}
return outputString;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Input String :");
String inputString = sc.nextLine();
String outputString = reverseTheSentence(inputString);
System.out.println("Input String : "+inputString);
System.out.println("Output String : "+outputString);
sc.close();
}
}
Explanation
Step 1 : Create one java.util.Scanner object to take input from the user.
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.String inputString = sc.nextLine();
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.String inputString = sc.nextLine();Step 3 : Split inputString into words and store them in a string array.
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.String inputString = sc.nextLine();Step 3 : Split inputString into words and store them in a string array.String[] words = inputString.split(“\\s”);
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.String inputString = sc.nextLine();Step 3 : Split inputString into words and store them in a string array.String[] words = inputString.split(“\\s”);Step 4 : Create one empty outputString object.
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.String inputString = sc.nextLine();Step 3 : Split inputString into words and store them in a string array.String[] words = inputString.split(“\\s”);Step 4 : Create one empty outputString object.String outputString = “”;
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.String inputString = sc.nextLine();Step 3 : Split inputString into words and store them in a string array.String[] words = inputString.split(“\\s”);Step 4 : Create one empty outputString object.String outputString = “”;Step 5 : Starting from last word of words array, append each word to outputString.
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.String inputString = sc.nextLine();Step 3 : Split inputString into words and store them in a string array.String[] words = inputString.split(“\\s”);Step 4 : Create one empty outputString object.String outputString = “”;Step 5 : Starting from last word of words array, append each word to outputString.for (int i = words.length-1; i >= 0; i–)
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.String inputString = sc.nextLine();Step 3 : Split inputString into words and store them in a string array.String[] words = inputString.split(“\\s”);Step 4 : Create one empty outputString object.String outputString = “”;Step 5 : Starting from last word of words array, append each word to outputString.for (int i = words.length-1; i >= 0; i–){
Step 1 : Create one java.util.Scanner object to take input from the user.Scanner sc = new Scanner(System.in);Step 2 : Take inputString from the user.String inputString = sc.nextLine();Step 3 : Split inputString into words and store them in a string array.String[] words = inputString.split(“\\s”);Step 4 : Create one empty outputString object.String outputString = “”;Step 5 : Starting from last word of words array, append each word to outputString.for (int i = words.length-1; i >= 0; i–){ Output String = output String + words[i] + ” “;
+ words[i] + ” “;}
+ words[i] + ” “;}Step 6 : Print output String.
.
. System.out.println(“Output String : “+output String);
String);Step 7 : Close the resources.
String);Step 7 : Close the resources.sc.close();
import java.util.Scanner;
public class StringManipulation {
public static void main(String[ ] args) {
// Accepting sentence and converting it into a String array.
System.out.print("Enter a Sentence - ");
String[ ] sentence = new Scanner(System.in).nextLine( ).split(" ");
// Printing sentence with the words in reversed order.
for (int i = sentence.length - 1; i >= 0; i- -)
System.out.print(sentence[i] + " ");
}
}