Introduction
Reversing a string is a common programming task that helps in understanding basic string manipulation techniques. In this task, the order of characters in a given string is reversed. For example, if the input string is “hello”, the output will be “olleh”. This operation can be performed using various methods in Java, such as using a loop, recursion, or built-in functions.
Java Program to Reverse a String
Below is a Java program that reverses a string using a loop:
public class ReverseString {
public static void main(String[] args) {
String input = "hello";
String reversed = reverseString(input);
System.out.println("Original String: " + input);
System.out.println("Reversed String: " + reversed);
}
public static String reverseString(String str) {
StringBuilder reversed = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
reversed.append(str.charAt(i));
}
return reversed.toString();
}
}
JavaHow the Code Runs
- Main Method: The
main
method is the entry point of the program. It defines a stringinput
with the value “hello”. - Reverse Method: The
reverseString
method is called, passing theinput
string as an argument. This method is responsible for reversing the string. - StringBuilder: Inside the
reverseString
method, aStringBuilder
object namedreversed
is created to hold the reversed string. - Looping: A
for
loop iterates over the string in reverse order, starting from the last character (indexstr.length() - 1)
to the first character (index0
). For each character, it appends the character to theStringBuilder
object. - Returning the Result: After the loop finishes, the
StringBuilder
is converted to a string usingtoString()
and returned to the calling method. - Output: Finally, the program prints the original string and the reversed string.
Explanation of Steps
Returning the Result: After constructing the reversed string, the StringBuilder
is converted back to a String
and returned.
StringBuilder: StringBuilder
is used to efficiently append characters to the string. It’s mutable, meaning it can change its content without creating a new object each time, which is more efficient than using a regular string in a loop.
Looping: The loop starts from the last character of the string and iterates backward. str.charAt(i)
is used to fetch each character at index i
.
Appending Characters: Each character obtained from the loop is appended to the StringBuilder
object using the append()
method.
How the Output is Produced
- Original String: The string
"hello"
is assigned to the variableinput
. - Reversed String: The
reverseString
method reverses the input string by iterating over it from the last character to the first and appending each character to aStringBuilder
. The reversed string"olleh"
is then returned and stored in thereversed
variable. - Printing: The program prints the original string and the reversed string to the console.
The output confirms that the program successfully reversed the input string "hello"
to "olleh"
.
Output
Original String: hello
Reversed String: olleh
JavaConclusion
Reversing a string is a fundamental operation that helps in understanding string manipulation and control flow in Java. This program demonstrates how to reverse a string using a simple loop and StringBuilder
, which are effective and efficient tools in Java for such operations. Understanding this basic concept lays the groundwork for tackling more complex string operations and algorithms in future programming tasks.
Learn Java From These Website
Certainly! If you want to learn Java, one excellent resource is the official Oracle Java Tutorials. These tutorials are comprehensive and cover everything from the basics to advanced topics.
Another great resource is GeeksforGeeks where you can find tutorials, practice problems, and detailed explanations on Java programming.
You can also explore W3Schools Java Tutorial for an easy-to-understand introduction to Java, including interactive examples and exercises.
These websites are great starting points for learning Java at your own pace!