Javatpoint Logo
Javatpoint Logo

Reverse a String Using a For Loop in Java

Reversing a string is a common project in programming, and it can be accomplished the use of diverse strategies. One such technique is with the aid of the use of a for loop in Java. In this text, we are able to discover how to reverse a string using a for loop and provide example program. We may even encompass code snippets with exact feedback to help you recognize the procedure.

Approach

The approach to reversing a string using a for loop involves iterating over the characters of the original string in reverse order and constructing a new string. The for loop will start from the last character of the input string and append each character to the new string. Let's dive into the code and example programs to demonstrate this process.

Example 1:

ReverseStringForLoopExample.java

Output:

Original String: Hello, World!
Reversed String: !dlroW ,olleH

In this situation program, we declare a variable enter and initialize it with the string we need to reverse, that is "Hello, World!". We additionally claim a variable reversed and assign an empty string to it. Then, we use a for loop to iterate over the characters of the input string in reverse order. The loop begins from input.Length() - 1 (the index of the ultimate character) and maintains until the index reaches zero. Inside the loop, we concatenate every character to the reversed string using the += operator. Finally, we print the original and reversed strings to the console.

Example 2:

ReverseStringForLoopExample.java

Output:

Original String: Hello, World!
Reversed String: !dlroW ,olleH

In this example, we've got a string with unique characters, " Hello, World!". Instead of the use of string concatenation with the += operator, we utilize the StringBuilder class to correctly build the reversed string. The StringBuilder elegance gives an append() technique to add characters to the string builder item. Once we finish the loop, we convert the StringBuilder object to a string the usage of the toString() technique and print the authentic and reversed strings.

Explanation:

Now, let's see the code snippets in extra detail with remarks to understand the system of reversing a string the use of a for loop.

String input = "Hello, World!";

String reversed = "";

In this section, we declare and initialize the input string with the desired value, which is "Hello, World!". We also declare a reversed string and assign an empty string to it. This variable will store the reversed string at the end of the process.

Here, we outline a for loop that iterates over the characters of the input string in reverse order. The loop begins from the index of the very last individual, that's input.Length() - 1, and maintains until the index reaches 0. The loop variable i represents the modern index. Inside the loop, we get right of entry to the individual at the ith index the use of the charAt() technique and concatenate it to the reversed string the usage of the += operator.

In this variation of the code, we use a StringBuilder item as opposed to concatenating strings using the += operator. The StringBuilder class affords better performance while dealing with string concatenation within loops. We initialize a StringBuilder item named reversed and use its append() approach to feature each character from the input string in reverse order.

Finally, we print the authentic and reversed strings to the console the usage of System.Out.Println(). To show the reversed string, we call the toString() approach on the StringBuilder object to transform it lower back to a normal string.

a string the use of a for loop. Now you've got a stable knowledge of how to reverse strings correctly in Java.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA