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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

20 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago