Hey so this is my code for checking for palindromes...it compiles fine, the problem is it doesnt work. Basically I converted the four sentences into strings without spaces or other characters and added them to a new string called newstr....i used the newstr to check for a palindrome in the latter part of my check palindrome method(after the first for loop), which works perfectly fine normally(when i dont have to remove spaces & non letter characters first)? By looking at my code do you see anything wrong or why it isnt finding the first 3 sentences as palindromes...thanks!

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;

public class Palindrome2 {

    //Variables
    static String newstr = "";
    static char[] letters;
    static char letters2;

    public static void main (String[] argv)
    {
        // Oldest known recorded palindrome.
        String str = "Evil did I dwell; lewd I did live";
        System.out.println ( str + " " + checkPalindrome(str) );
        //reset newstr after checking each sentence
        newstr = "";
        // Oldest reference.
        str = "Madam, I'm Adam";
        System.out.println ( str + " " + checkPalindrome(str) );
        newstr = "";
        // One of the most famous.
        str = "A man, a plan, a canal: Panama";
        System.out.println ( str + " " + checkPalindrome(str) );
        newstr = "";
        // Not a palindrome, but a palingram:
        str = "He was, was he?";
        System.out.println ( str + " " + checkPalindrome(str) );
        newstr = "";
    }


    static String checkPalindrome (String str)
    {
        // Extract the letters.
        char[] letters = str.toCharArray ();

        //iterate through all the char in the sentences
        int len = letters.length;
        for(int i =0; i<len; i++){
            //checks if current char is a letter and adds each char to newstr
            if(Character.isLetter(letters[i])){
                letters2 = letters[i];
                newstr += letters2;
            }
        }

        // Extract the letters.
        letters = newstr.toCharArray ();
System.out.print(letters);
        // Create an empty stack.
        Stack<Character> stack = new Stack<Character>();

        // The letters must "balance" up to the middle.
        int mid = letters.length / 2;

        // Push the first half.
        for (int i=0; i<mid; i++) {
            stack.push (letters[i]);
        }
        // Odd or even? We have to adjust the mid-point accordingly.
        if (letters.length % 2 > 0) {
            // Odd number => swallow middle letter.
            mid = mid+1;
        }

        // Now check the second half.
        for (int i=mid; i<letters.length; i++) {
            char ch = stack.pop ();
            if (ch != letters[i]) {
                // Mismatch => not a palindrome.
                return "is not a palindrome";
            }
        }
        return "is a palindrome";
    }
}

Add a println to show what is being compared so you can see what the computer is seeing.

@norm i was doing that and it appeared perfectly fine....i forgot about checking for lowercase too

After I added the println, the case showed up immediately as the problem.
...
i=23 ch=v, letters[i]=v
i=24 ch=E, letters[i]=e <<<< NOTE HERE
Evil did I dwell; lewd I did live is not a palindrome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.