Reverse a string - Coder's point

Saturday, March 16, 2019

Reverse a string

 import java.util.*;  
 class reverse_string  
 {  
   String str;  
   void reverse()  
   {  
     String temp="";  
     for(int i=str.length()-1;i>=0;i--)  
     {  
       temp=temp+str.charAt(i);  
       }  
     System.out.println(temp);  
   }  
   public static void main()  
   {  
     Scanner in = new Scanner(System.in);  
     reverse_string obj=new reverse_string();  
     System.out.println("Enter a string");  
     obj.str=in.nextLine();  
     obj.reverse();  
   }  
 }  
OUTPUT:
Enter a string
sentence
ecnetnes

No comments:

Post a Comment