-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome.Java
More file actions
32 lines (22 loc) · 1.17 KB
/
Palindrome.Java
File metadata and controls
32 lines (22 loc) · 1.17 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
package com.kopans;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter word:"); //Asks for a word
String origString = myObj.nextLine(); // Read user input
String input = origString.toLowerCase(); //changes variable name and makes the word lowercase since Java is case sensitive
int length = input.length(); //sets length to the length of the input
boolean isPalindrome = true; //starts off assuming that it is a palindrome
for(int beginIndex = 0; beginIndex < length; beginIndex++) //for loop going through word
{
if(input.charAt(beginIndex) != input.charAt(length-1-beginIndex)) { //checking if the characters are not equivalent
System.out.println(origString+ " is not a palindrome.");
isPalindrome = false;
break;
}
}
if(isPalindrome) { //If Boolean isPalindrome is still true prints that it is a palindrome
System.out.println(origString+ " is a palindrome.");
}
}}