String

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

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

  • 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

Output 2

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

Output 3

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

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
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago