Sunday 8 May 2016

JAVA program for reversing the words in the given sentence


Explanation:

User give an input as a sentence. That contains set of words separated by space. We have to reverse the each word in the given input sentence.

Input format:

Enter any sentence:
Let Us Code in C Let Us Code in JAVA

Output format:

teL sU edoC ni C teL sU edoC ni AVAJ

Program:

import java.util.Scanner;
public class Test{
           public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.println("Enter any Sentence:");
      String ss = s.nextLine( );
      String[ ] a = ss.split(" ");
      String res="";
      for(int i=0;i<a.length;i++){
         StringBuffer s1 = new StringBuffer(a[i]);
         s1.reverse( );
         res+=s1+" ";
      }
      System.out.println(res);
    }
}


Source Code: download


0 comments:

Post a Comment