Heres my code. I'm not sure whats wrong. My project is to create a program that checks if a word is a palindrome.
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
// Open Scanner
Scanner input = new Scanner(System.in);
// Prompt User for word
System.out.println("Enter word to check if it is a Palindrome:");
// Scan in the word
String word = input.nextLine();
int a = 0; // used to extract word from array (small)
int b = 0; // used to extract word from array (large)
int c = 0; // used to stop when
int d = (word.length());
int e = d / 2;
int f = e - 2;
int x = word.length(); // set cap of array pulling
char[] array = word.toCharArray(); // create array of chars
if (array[a] == array[x] && c != f) {
a++;
x--;
c++;
} else {
b = 1;
}
if (b == 1) {
System.out.println("This word is not a Palindrome!");
} else if (b == 0) {
System.out.println("This word is a Palindrome!");
}
}
}
The error is at the
if (array[a] == array[x] && c!=f)
I'm not exactly sure what went wrong but when you put in a non-palindrome it skips over. I'd be more than glad to have some advice as to what to do in this situation.