-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaReverseString.java
More file actions
52 lines (37 loc) · 998 Bytes
/
Copy pathJavaReverseString.java
File metadata and controls
52 lines (37 loc) · 998 Bytes
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Question:
A string is called a palindrome if it reads the same
forward and backward.
Given a string, print "Yes" if it is a palindrome,
otherwise print "No".
Example:
Input:
madam
Output:
Yes
*/
import java.util.*;
public class Solution {
public static void main(String[] args) {
// Create Scanner object to take input
Scanner sc = new Scanner(System.in);
// Read the input string
String s = sc.next();
// Variable to store reversed string
String rev = "";
// Close Scanner
sc.close();
// Reverse the string
for (int i = s.length() - 1; i >= 0; i--) {
rev = rev + s.charAt(i);
}
// Check if original string and reversed string are equal
if (s.equalsIgnoreCase(rev)) {
// String is a palindrome
System.out.println("Yes");
} else {
// String is not a palindrome
System.out.println("No");
}
}
}