Write a program to input a string and print each word in reverse.
import java.io.*;class reverse{ public static void main() throws IOException { InputStreamReader ir=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(ir); //instance variables-replace the example below with your own. int i,a1,l,p,j; String str; System.out.println("Enter the String: "); str=br.readLine(); //Constructor for objects of class reverse p=0; //initialise instance variables l=str.length(); System.out.println("The Original String is: "+str); System.out.println("The Reversed String with Word Position in place is: \n"); for(i=0;i < l;i++) { char a=str.charAt(i); a1=(int)a; if(a1==32 || i==l-1) { for(j=i;j >= p;j--) { char b=str.charAt(j); System.out.print(b); } System.out.print(" "); p=i; } } }}//IF ANY ERRORS IN CODE PLEASE COMMENT BELOW |
No comments:
Post a Comment