How to reserve a string in Java without using reverse function

There are following ways to reverse a string in Java:

  • Using for loop
  • Using While loop
  • Using static method

Using For loop

Example to reverse string in Java by using for loop

In the following example, we have used for loop to reverse a string. The for loop executes until the condition i>0 becomes false.

  1. import java.util.Scanner;  
  2. class ReverseStringExample1  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. String s;  
  7. Scanner sc=new Scanner(System.in);  
  8. System.out.print(“Enter a String: “);  
  9. s=sc.nextLine();                    //reading string from user  
  10. System.out.print(“After reverse string is: “);  
  11. for(int i=s.length();i>0;–i)                //i is the length of the string  
  12. {  
  13. System.out.print(s.charAt(i-1));            //printing the character at index i-1  
  14. }  
  15. }  
  16. }  

Output:

How to reserve a string in Java without using reverse function

Using while loop

Example to reverse string in Java by using while loop

In the following example, i is the length of the string. The while loop execute until the condition i>0 becomes false i.e. if the length of the string is 0 then cursor terminate the execution. It prints the characters of the string which is at index (i-1) until i>0.

  1. import java.util.Scanner;  
  2. class ReverseStringExample2  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. String s;  
  7. Scanner sc=new Scanner(System.in);                    //reading string from user  
  8. System.out.print(“Enter a String: “);  
  9. s=sc.nextLine();  
  10. System.out.print(“After reverse string is: “);  
  11. int i=s.length();                   //determining the length of the string   
  12. while(i>0)  
  13. {  
  14. System.out.print(s.charAt(i-1));                 //printing the character at index i-1  
  15. i–;                               //decreasing the length of the string  
  16. }  
  17. }  
  18. }  

Output:

How to reserve a string in Java without using reverse function

Using Static method

Example to reverse string in Java by using static method

In the following example, we have created an object of the class and called the static method with the object as rev.reverse(s) by passing the given string.

  1. import java.util.Scanner;  
  2. public class ReverseStringExample3  
  3. {  
  4. public static void main(String[] arg)  
  5. {  
  6. ReverseStringExample3 rev=new ReverseStringExample3();  
  7. Scanner sc=new Scanner(System.in);  
  8. System.out.print(“Enter a string : “);  
  9. String  s=sc.nextLine();      
  10. System.out.println(“Reverse String  is : “+rev.reverse(s)); //calling method  
  11. }  
  12. //calling method   
  13. static String reverse(String str)  
  14. {  
  15. String rev=””;  
  16. for(int i=str.length();i>0;–i)  
  17. {  
  18. rev=rev+(str.charAt(i-1));   
  19. }  
  20. return rev;  
  21. }  
  22. }  

Output:

How to reserve a string in Java without using reverse function

Leave a Comment