-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringReversal83.java
30 lines (23 loc) · 1.02 KB
/
StringReversal83.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class StringReversal83 {
// Method to reverse a given string
public static String reverseString(String str) {
if (str == null || str.isEmpty()) {
return str; // Return the input string if it is null or empty
}
StringBuilder reversed = new StringBuilder();
// Append characters from the end of the string to the StringBuilder
for (int i = str.length() - 1; i >= 0; i--) {
reversed.append(str.charAt(i));
}
return reversed.toString(); // Convert StringBuilder to String and return
}
// Main method to execute the program
public static void main(String[] args) {
String inputString = "Hello, World!";
// Call the reverseString method and store the result
String reversedString = reverseString(inputString);
// Display the reversed string
System.out.println("Original String: " + inputString);
System.out.println("Reversed String: " + reversedString);
}
}