Hello I’m trying to make a program that check first letter of a word and see if it equals the last letter.
public class isPalindrome
{
public static void main(String[] args)
{
// Create an array of strings to test.
String[] testStrings = { "Able was I ere I saw Elba",
"Rats live on no evil star",
"Four score and seven years ago",
"Barrack Obama",
"Now is the time for all good men",
"Desserts I stressed",
"Ask not what your country can do for you",
"Kayak",
"Vegeta",
"A Man, A Plan, a canal, Panama!"};
for (int i = 0; i < testStrings.length; i++)
{
System.out.print("\"" + testStrings[i] + "\"");
if (Palindrome(stripString(testStrings[i])))
System.out.println(" is a palindrome.");
else
System.out.println(" is not a palindrome.");
}
}
public static String stripString(String strip)
{
strip = strip.toUpperCase();
String stripped= "";
for (int i= 0; i< strip.length(); i++)
{
if (Character.isLetter(strip.charAt(i)))
stripped += strip.charAt(i);
}
return stripped;
}
public static boolean Palindrome(String str)
{
boolean status = false;
if (str.length() <= 1)
status = true;
else if (str.charAt(0) == str.charAt(str.length()-1))//Recursive Case
{
status = Palindrome (str.substring (1, str.length()-1));
}
return status;
}
}
Yes it does:
Could it be that you’re not running it after compiling it? This command compiles it:
and then this one runs it:
Edit: Or you may need:
…to tell Java to look for classes in the current directory. (I don’t in my largely un-customized Java install, but you used to have to, so…)
Off-topic: This is nothing to do with the problem, but: In Java, the coding convention (which you’re free to ignore) is that class names start with a capital letter, and method names start with a lower-case letter. So the class would be
IsPalindrome(orPalindromeTesteror something), and then the methodPalindromewould beisPalindromeor similar. If you use the convention, it’ll be easier to work with other people, because you won’t confuse them with unusual naming of things. 🙂