Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Tuesday, 16 August 2016

java program to print sentence in reverse order of words..

import java.util.Scanner;
public class Reverse {
public static void main(String []argv)
{System.out.println("enter the string");
Scanner s1=new Scanner(System.in);
    String s=s1.nextLine();
    String a[]=s.split(" ");
      String finalStr="";
      for(int i = a.length-1; i>= 0 ;i--)
      {
          finalStr += a[i]+" ";
      }
     
System.out.println("String before reverse -s => :  " +s);
System.out.println("string after reversal of words => ");
System.out.println(finalStr);
s1.close();
}
}

No comments:

Post a Comment