Codeforcoding

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 using loops
Reversed 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

Output 1

Explanation

 

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

Output 2

Explanation

 

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

Output 3

Explanation

 

Suggested post

String manipulation in C language

String function in C language

For loop in Java language

While loop in Java 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

 

C++ program to create reverse of a string using loops
Java program to reverse a number using loops
Exit mobile version