import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse"); original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: "+reverse);
}
}
//Using Internal java Method
class InvertString
{
public static void main(String args[])
{
StringBuffer a = new StringBuffer("Java programming is fun"); System.out.println(a.reverse());
}
}
Class 1: ReverseString (Manual Method)
This class explains how one could reverse a string manually character by character using a for loop.
Step-by-Step Explanation:
1. Importing Scanner Class:
import java.util.*;
This imports the Scanner class, which is used to take user input.
2. Class Definition:
class ReverseString
The class ReverseString is defined. This class contains the main method where the program execution starts.
3. Main Method:
public static void main(String args[])
The main method is the starting point for the program. This is where the program will run.
4. Variable Declarations:
String original, reverse = “”;
Original: This variable will hold the original string typed by the user.
reverse: This variable will store the reversed string. It is initially set to an empty string.
5. Creating a Scanner Object to Read Input:
Scanner in = new Scanner(System.in);
System.out.println(“Enter a string to reverse”);
original = in.nextLine();
- A Scanner object named in is created to read user input from the console.
- The program requests a string as an input. This input is saved in the original variable.
6. Obtaining Length of the String:
int length = original.length();
Here, length() function is used to retrieve the number of characters input by the user. This length is saved to the variable length.
7. Upside-Down String Using a Loop:
for (int i = length – 1; i >= 0; i–)
reverse = reverse + original.charAt(i);
- The for loop starts at the last character of the string (i = length – 1) and goes backwards (i–).
- original.charAt(i) returns the character at position i in the string.
- This character is appended to reverse, which is the reverse of the input one character at a time.
- This loop iterates till it reaches the first character (i >= 0).
8. Printing the Reversed String:
System.out.println(“Reverse of entered string is: ” + reverse);
- Once the loop is completed, the string reverse holds the reverse of the inputted string.
- The reversed string is printed out.
Sample Input and Output for ReverseString:
Input:
Enter a string to reverse
Hello
Output:
Reverse of entered string is: olleH
Class 2: InvertString (Using StringBuffer Method)
This class demonstrates how to reverse a string using Java’s built-in StringBuffer class, which has a reverse() method.
Step-by-Step Explanation:
1. Class Definition:
class InvertString
The class InvertString is defined. This class contains the main method where the program execution starts.
2. Main Method:
public static void main(String args[])
The main method represents the entry of the program where the program runs.
3. Creating StringBuffer Object:
StringBuffer a = new StringBuffer(“Java programming is fun”);
A StringBuffer object a is defined with the string “Java programming is fun”. In Java, StringBuffer is a mutable class, so after being created, it can be changed.
4. Reversing the String Using StringBuffer’s reverse() Method:
System.out.println(a.reverse());
- StringBuffer calls the reverse() method on StringBuffer object a to reverse its contents.
- The reversed string is then printed using System.out.println().
Output for InvertString Example
Output:
nuf si gnimmargorp avaJ
Summary of Both Classes:
- Uses a manual approach with a for loop to reverse the string by accessing each character from the end to the beginning and appending it to a new string.
- Uses the StringBuffer class, which has a built-in method (reverse()) that makes reversing the string easier without having to manually process each character.
Key Differences:
- Manual Approach: In the ReverseString class, the string is reversed by manually looping over the characters, which is more flexible but noisier.
- Internal Method (StringBuffer): In the case of class InvertString, the implementation using StringBuffer.reverse() is shorter and much easier to use to invert a string.
Time Complexity:
- Both methods, whether using the explicit loop or the StringBuffer.reverse() method are time complexity of \\(O(n)\\) where n is the number of characters in the string since each method iterates over each character of the string once.
- For both classes, space complexity is \\(O(n)\\) because they save the reversed string; it needs space proportional to the size of the input string
Conclusion:
Both classes represent how to reverse a string in Java. It can be easily seen that the class using StringBuffer represents a more compact implementation with the inherent functionality of a class.