Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double string comparison using recursion

Tags:

java

I'm having some trouble with a question from my programming II class and have hit a brick wall, was wondering if someone could help?

The question asks for a user to input a string, the program to reverse the input string and then to compare the reverse to the original, this must be done recursively.

So far I have:

public class question1 
{
public static void main(String args[])  
{

String input = JOptionPane.showInputDialog(null, "Please enter a sentence to determine if it is a palindrome.");
String backwardsinput = Reverse(input);
System.out.println(backwardsinput);
boolean Palindrome = PalindromeCheck(backwardsinput, input);

    if (Palindrome == true)

        {
        JOptionPane.showMessageDialog(null,"That is a palindrome!");
        }

    if (Palindrome == false)

        {
        JOptionPane.showMessageDialog(null,"That is not a palindrome"); 
        }

}

public static String Reverse (String input)
{
    if (input.length() <= 1)
    return input;

    else
    {
        char x = input.charAt(input.length()-1);               
        return x+Reverse(input.substring(0,input.length()-1));
    }

}



public static boolean PalindromeCheck (String backwardsinput, String input)
{

     if(input.length() == 0 || input.length() == 1)
            return true;

        if(backwardsinput.charAt(0) == input.charAt(input.length()-1))
            return PalindromeCheck(backwardsinput.substring(1, backwardsinput.length()-1), input.substring(1, input.length()-1));

        else
        return false;

       }    
}

My problem is, it tells me everything is a palindrome, I've looked at it over and over and can't figure out why!

like image 738
Eogcloud Avatar asked Oct 24 '11 11:10

Eogcloud


People also ask

How do you compare 2 strings?

String strcmp() function in C++ In order to compare two strings, we can use String's strcmp() function. The strcmp() function is a C library function used to compare two strings in a lexicographical manner. The function returns 0 if both the strings are equal or the same.

Can you use == to compare two strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

What happens when you compare two strings with the == operator?

String Comparison With Objects Class The method returns true if two Strings are equal by first comparing them using their address i.e “==”. Consequently, if both arguments are null, it returns true and if exactly one argument is null, it returns false.

What is string comparison method?

Java String compareTo() MethodThe method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).


1 Answers

You're doing the work twice (sort of).

if(backwardsinput.charAt(0) == input.charAt(input.length()-1))
                                            ^^^^^^^^^^^^^^^^

should be

if (backwardsinput.charAt(0) == input.charAt(0))
                                             ^

You almost got it :-)


Also, another way of expressing

if (cond)
    return something;
else
    return false;

is

return cond && something;

thus your last lines could be written as

return backwardsinput.charAt(0) == input.charAt(0) &&
       palindromeCheck(backwardsinput.substring(1, backwardsinput.length() - 1),
                                input.substring(1, input.length() - 1));

Related question / answer:

  • How to find out all palindromic numbers
like image 118
aioobe Avatar answered Sep 29 '22 02:09

aioobe