Q - You are given a string s. You need to reverse the string.
GeekForGeeks Problem - Link | Click Here
class Reverse {
// Method to reverse a given string
public static String reverseWord(String str) {
// Get the length of the input string
int len = str.length();
// Create a StringBuilder to construct the reversed string
StringBuilder reversed = new StringBuilder();
// Traverse the input string from end to start
for (int i = len - 1; i >= 0; i--) {
// Append each character to the StringBuilder in reverse order
reversed.append(str.charAt(i));
}
// Convert the StringBuilder to a string
String reversedString = reversed.toString();
// Return the reversed string
return reversedString;
}
}