Reverse a String in Java Using a While Loop19 Jun 2025 | 4 min read When operating with strings in Java, it is regularly necessary to reverse the order of characters within a string. Reversing a string can be carried out in various methods, and one not unusual approach is to use while loop. In this context, we are able to discover how to reverse a string the usage of sometime loop in Java, supplying distinct examples. Before diving into the code, understand the logic in the back of reversing a string the use of some while loop. The idea is to begin from the final character of the input string and iterate until we attain the primary character. During each iteration, we append the cutting-edge character to a new string variable to be able to hold the reversed string. By reversing the order of characters in this way, we effectively obtain the reversed string. Example 1:ReverseStringExample.java Output: Original string: Hello, World! Reversed string: !dlroW ,olleH In this example, we've a reverseString approach that takes an enter string as a parameter and returns the reversed string. Inside the technique, we initialize the reversedString variable as an empty string. The index variable is ready to the remaining index of the enter string, acquired by way of subtracting 1 from the length of the string. The while loop runs so long as the index is greater than or identical to 0. Within each generation, we append the man or woman at the present day index to the reversedString variable using the += operator. After appending the character, we decrement the index by 1 to move to the preceding character inside the string. This procedure keeps until we attain the primary character of the enter string. In the main method, we provide a sample input string, "Hello, World!", and call the reverseString method to obtain the reversed string. Finally, we print both the original and reversed strings to the console. As we can see, the original string "Hello, World!" is successfully reversed to "!dlroW ,olleH" using the while loop approach. The while loop technique to reverse a string in Java can handle strings of any length. It is a direct and efficient approach that makes use of fundamental string manipulation operations. However, it's well worth noting that concatenating strings the use of the += operator inner a loop can be inefficient for huge strings because of the immutable nature of strings in Java. In such cases, using a StringBuilder or StringBuffer might be a more efficient choice. Let's adjust our preceding example to use a StringBuilder instead: ReverseStringExample.java Output: Original string: Hello, World! Reversed string: !dlroW ,olleH In this modified example, we replaced the reversedString variable with a StringBuilder object. Inside the while loop, we use the append method of the StringBuilder class to efficiently concatenate the characters. By using a StringBuilder, we ensure better performance when dealing with larger strings. In Summary, reversing a string the use of a while loop in Java is a easy and effective method. By iterating through the characters in reverse order and appending them to a new string or a StringBuilder object, we are able to obtain the reversed string. While the even as loop approach showcased here is suitable for smaller strings, for larger strings, it is recommended to use a StringBuilder or StringBuffer for better overall performance. |
In Java, List is is an interface of the Collection framework. It provides us to maintain the ordered collection of objects. The implementation classes of List interface are ArrayList, LinkedList, Stack, and Vector. The ArrayList and LinkedList are widely used in Java. In this section, we...
4 min read
The noneMatch() method in Java's Stream API is an essential function employed to evaluate whether none of the elements in a given stream satisfy a particular condition. It is particularly useful when we need to prove that none of the items in a collection match...
11 min read
The problem under consideration refers to the determination of the maximum product of the values of the nodes in any path present in a binary tree. A path is considered to be a sequence of nodes starting from a particular node to any other node in...
5 min read
The java.text.RuleBasedCollator class has getCollationElementIterator() function. The object of the collation element iterator for the supplied character iterator object is obtained using the RuleBasedCollator class. Syntax: public CollationElementIterator getCollationElementIterator(CharacterIterator source) Parameter: The character iterator object can be passed as an argument to this function. Return Value: The...
3 min read
Constraints play a crucial role in Java programming, helping developers define rules and conditions that must be met for their code to function correctly. They ensure data integrity, protect against unexpected behavior, and contribute to the overall robustness of Java applications. In this section, we will...
8 min read
In Java programming, determining if two matrices are mirror images of each other involves comparing corresponding elements in reverse order. A matrix is considered a mirror image of another if its rows or columns are reversed versions of the corresponding rows or columns in the...
6 min read
AbstractSet in Java In Java, the AbstractSet class is a member of the Java Collections Framework and extends the AbstractCollection class. It represents an abstract set, which is an unordered collection of elements with no duplicate values. A set is a data structure that only allows a...
22 min read
A number n is given. Our task is to find the total number of trailing zeros that are present in the value of the factorial of n. See the following examples for a better understanding. Example: 1 Input: int n = 6 Output: 1 Explanation: The factorial of the number 6...
7 min read
In this section, we will learn what is Kynea number and also create Java programs to that calculates the Kynea number. The Kynea number program is frequently asked in Java coding interviews and academics. Kynea numbers are numbers that are recursively defined as: F(k) = 4 x F(k...
6 min read
Top 15 Frameworks are written in a programming language. In Java, we utilize frameworks to create applications using the Java programming language. The reusable code written in Java, which serves as a template, is referred to as a Java Framework. We develop apps by integrating...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India