-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse a Str.java
More file actions
22 lines (18 loc) · 1.01 KB
/
Reverse a Str.java
File metadata and controls
22 lines (18 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner; // Importing the Scanner class for potential user input (currently unused but can be utilized for dynamic input in the future)
class Main {
public static void main(String[] args) {
// Step 1: Define the input string to be reversed
String str = "PIEMR"; // Predefined string to demonstrate reversing functionality
// Step 2: Print a message indicating the result
System.out.print("String after reversed:"); // Message to inform the user about the reversed string
// Step 3: Loop through the string in reverse order
// The loop starts from the last character (str.length() - 1)
// and continues until the first character, decrementing the index (i) in each iteration
for (int i = str.length() - 1; i >= 0; i--) {
// Print each character of the string in reverse order
System.out.print(str.charAt(i));
}
// Step 4: Print a newline at the end for cleaner output
System.out.println();
}
}