Tim Carew
Tim Carew

Reputation: 31

Using recursion to test if two strings are the same but reversed

For my assignment I have to use recursion to test if two strings are the same but reversed.

Here is the actual question:

Write a recursive method called isStringReversed which, given two String parameters, returns true if the two strings contain the same sequence of characters but in reverse order (ignoring capitalization), and false otherwise. For example, isStringReversed("piano", "oNaIp") would return true. Note: the null (empty) string and string of one letter are both the reverse of themselves.

Here's what I have made, but when I execute the program, i always get a false return. Heres my code:

public static boolean isStringReversed(String s1, String s2) {
    if (s1 == null || s2 == null || s1.length() == 1 || s2.length() == 1) {
        return true;
    }else if (s1.length() != s2.length()) {
        return false;
    }else {
        char s1first = Character.toLowerCase(s1.charAt(0));
        char s2last = Character.toLowerCase(s2.charAt(s2.length() - 1));

        if (s1first == s2last){
            String s1shorter = s1.substring(0, s1.length() - 1);
            String s2shorter = s2.substring(0, s2.length() - 1);
            return isStringReversed(s1shorter, s2shorter);
        }else {
            return false;
        } 
    }
}

Upvotes: 0

Views: 3936

Answers (3)

Rok Povsic
Rok Povsic

Reputation: 4935

When doing substring on s1first, you should remove the first character, not the last.

Just one line above that you compare first character of s1first and last character of s2last. So once you establish that they are equal, that's what you remove and continue the recursion.

Upvotes: 3

Zinov
Zinov

Reputation: 4119

This is the recursive idea

        public static bool isStringReversed(string input1, string input2)
        {
            string s1 = input1.ToLower();
            string s2 = input2.ToLower();

            if(s1 == null && s2 == null) 
                return true;
            if (s1.Length == 1 && s2.Length == 1)
            {
                if (s1 == s2)
                    return true;
                return false;
            }

            if (s1.Length != s2.Length)
                return false;

            return isStringReversedRec(s1, s2, s1.Length - 1, 0);

        }

        private static bool isStringReversedRec(string s1, string s2, int indexS1, int indexS2)
        {
            if (indexS1 < 0)
                return true;

            if (s1.charAt(indexS1) != s2.charAt(indexS2))
                return false;

            return isStringReversedRec(s1, s2, indexS1 - 1, indexS2 + 1);
        }

Upvotes: -1

MarianD
MarianD

Reputation: 14191

Instead of

    String slshorter = sl.substring(0, s1.1ength() - l) ;
    String s2shorter = s2.substring(0, s2.1ength() - l) ;

use

    String slshorter = sl.substring(1) ;
    String s2shorter = s2.substring(0, s2.1ength() - 1) ;

Note that the second parameter of the method substring() means to that index exclusive.

Upvotes: 1

Related Questions