-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome.java
More file actions
32 lines (30 loc) · 802 Bytes
/
Palindrome.java
File metadata and controls
32 lines (30 loc) · 802 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
/**
*Program to test if an input string is a palindrome
*@author Michael Roscoe
*@author Kohdy Nicholson
**/
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
String x;
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a string: ");
x = scan.nextLine();
if (stringCompare(x)) {
System.out.println("This string is a palindrome: '" + x + "'");
} else {
System.out.println("This string is not a palindrome: '" + x + "'");
}
}
public static boolean stringCompare(String s) {
boolean success = false;
if (s.length() == 0 || s.length() == 1) {
success = true;
} else {
if (s.charAt(s.length()-1) == s.charAt(0)) {
success = stringCompare(s.substring(1,s.length()-1));
}
}
return success;
}
}