-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse.java
More file actions
17 lines (17 loc) · 773 Bytes
/
reverse.java
File metadata and controls
17 lines (17 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
public class reverse {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = scanner.nextLine();
// Create a new character array to store the reversed string
char[] reversed = new char[str.length()];
// Start from the last character of the string and copy each character to the reversed array
for (int i = str.length() - 1, j = 0; i >= 0; i--, j++) {
reversed[j] = str.charAt(i);
}
// Convert the character array to a string and print it
String reversedStr = new String(reversed);
System.out.println("Reversed string: " + reversedStr);
}
}