-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Reverse.java
More file actions
18 lines (17 loc) · 859 Bytes
/
String_Reverse.java
File metadata and controls
18 lines (17 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// WAP to reverse a String
import java.util.Scanner;
public class String_Reverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String: ");
String str = sc.nextLine();
sc.close();
str = str.toLowerCase().trim(); // to remove leading and trailing spaces and make it lowercase to ensure uniformity and case sensitiveness
String reversedStr = "";
for (int i = 0; i < str.length(); i++) {
// reversedStr += str.charAt(i); // this will not append but will add at the end of the string meaning it will not reverse the string
reversedStr = str.charAt(i) + reversedStr; // Prepend each character to the reversed string
}
System.out.println("Reversed String: " + reversedStr);
}
}