-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSc2720_Palindrome.java
More file actions
50 lines (36 loc) · 1.28 KB
/
CSc2720_Palindrome.java
File metadata and controls
50 lines (36 loc) · 1.28 KB
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
//Program Name: Palindrome/Lab 1
//Name: Stephanie L. Wyckoff
//Date: 1/13/2017
//Course: Data Structures Lab
//Professor: Duan
//TA: Pelin Icer
//Description: This program takes in a String from the user and checks if that String is a palindrome, the same spelling both forwards and backwards.
// The program output is a statement of whether the String was or was not a palindrome.
import java.util.Scanner;
public class Palindrome {
//Method checks whether or not input String is a palindrome;
public static boolean checkPalindrome(String s){
s=s.toLowerCase();
for(int i = 0; i < (s.length()/2); i++){
if(s.charAt(i) != s.charAt(s.length()-i-1)){
return false;
}
}return true;
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
//Request for user to enter a word
System.out.println("Please enter a word: ");
//assigns user input String to input
String input = s.nextLine();
//if statement calls checkPalindrome(input) and checks if outcome of the method is true or false
if(checkPalindrome(input) == true){
System.out.println(input + " is a palindrome.");
}
else{
System.out.println(input + " is not a palindrome.");
}
//closes Scanner
s.close();
}
}