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
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
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
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
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
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…