-
Notifications
You must be signed in to change notification settings - Fork 153
/
Palindrome2.java
32 lines (27 loc) · 991 Bytes
/
Palindrome2.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
31
32
/**
* @author MadhavBahl
* @date 21/12/2018
*/
import java.util.Scanner;
// Method 2: Check whether reversed string is equal to the original string
public class Palindrome2 {
public static void main (String[] args) {
System.out.println("/* ===== Palindrome Check ===== */");
System.out.print("\nPlease enter a string to check: ");
// Read the string
Scanner input = new Scanner(System.in);
String str = input.next();
String reversed = "";
int size = str.length();
// Reverse the string
for (int i=0; i<str.length(); i++) {
reversed = str.charAt(i) + reversed;
}
// Check if the reversed string is same as the original string
if (str.equals(reversed)) {
System.out.println ("The given string \"" + str + "\" is a Palindrome");
} else {
System.out.println ("The given string \"" + str + "\" is not a Palindrome");
}
}
}