Java code to reverse a string using loops
Java code to reverse a string using loops
In this tutorial, we will discuss the concept of Java code to reverse a string using loops
In this post, we are going to learn how to reverse every word of the given string by the user and display the reversed string as an output
here, we are used for loop, while loop and do-while loop for display reversed string of the given string
Java code to reverse a string
Display reversed string using for loop
Program 1
The program request the user to enter a string and the program displays the reversed string of the given string using for loop in Java language
import java.util.Scanner; class ReverseStringForloop{ public static void main(String args[]){ String str;//declare a String variable char ch;//declare a char variable System.out.println("Enter a String as you wish: "); //ask input from the user Scanner scan=new Scanner(System.in); //cretae scanner object str =scan.nextLine(); System.out.println("Reserve of given String "+str+" is : "); for(int j=str.length(); j>0; j--){ System.out.print(str.charAt(j-1)); } } }
When the above code is executed, it produces the following result
Explanation
- The user is asked to enter a string and it is stored in the character variable of ‘str’
- The length of the string is assigned to variable ‘J’
- The for loop is used to create the reversed string
- The for loop is functioning until ‘j’ is greater than zero
- Finally, the output is displayed as the reversed string
Display reversed string using while loop
Program 2
The program request the user to enter a string and the program displays the reversed string of the given string using while loop in Java language
import java.util.Scanner; class ReverseStringwhileloop{ public static void main(String args[]){ String str; char ch; System.out.println("Enter a String as you wish: "); //ask input from the user Scanner scan=new Scanner(System.in); //cretae scanner object str =scan.nextLine(); System.out.println("Reserve of given String "+str+" is : "); int j=str.length(); while(j>0){ System.out.print(str.charAt(j-1)); j--; } } }
When the above code is executed, it produces the following result
Explanation
- The user is asked to enter a string and it is stored in the character variable of ‘str’
- The length of the string is assigned to variable ‘J’
- The while loop is used to create the reversed string
- The while loop is functioning until ‘j’ is greater than zero
- Finally, the output is displayed as the reversed string
Display reversed string using do-while loop
Program 3
The program request the user to enter a string and the program displays the reversed string of the given string using do-while loop in Java language
import java.util.Scanner; class ReverseStringDowhileloop{ public static void main(String args[]){ String str; char ch; System.out.println("Enter a String as you wish: "); //ask input from the user Scanner scan=new Scanner(System.in); //cretae scanner object str =scan.nextLine(); System.out.println("Reserve of given String "+str+" is : "); int j=str.length(); do{ System.out.print(str.charAt(j-1)); j--; }while(j>0); } }
When the above code is executed, it produces the following result
Explanation
- The user is asked to enter a string and it is stored in the character variable of ‘str’
- The length of the string is assigned to variable ‘J’
- The do-while loop is used to create the reversed string
- The do-while loop is functioning until ‘j’ is greater than zero
- Finally, the output is displayed as the reversed string
Suggested post
String manipulation in C language
Do-while loop in Java language
Similar post
C++ program to display reversed string
C program to display reversed string
Java program to display reversed string