user10421396
user10421396

Reputation:

Compare two String using recursion (case insensitive)

I needed to Write a recursive method to compare two Strings using alphabetical order without using compareTo.

string1 comes before string2 returns an integer less than 0
string1 == (or indistinguishable from) string2 returns 0
string1 comes after string2 returns an integer greater than 0

I have written a method that works just fine, the problem is that if I compare two similar string or a string to itself it returns 1 instead of 0.

Any idea how can I optimize my method so it is not too long and does not fail to compare two identical strings?

I think part of my problem is because I declared my variable static, but not sure how I should work it out to declare them inside the method.

Code:

     public class test{

            public static String s1 = "alpha";
            public static String s2 = "delta";
            public static String s3 = "omega";
            public static String s4 = "alpha";
            public static int  result;

            public static void main (String[]args){

                System.out.println(recursiveCompare(s1,s2));  // -1  good
                System.out.println(recursiveCompare(s3,s1));  //  1  good
                System.out.println(recursiveCompare(s4,s1));  //  1  FAIL!!! should be 0
                System.out.println(recursiveCompare(s2,s3));  // -1  good
                System.out.println(recursiveCompare(s1,s1));  // -1  FAIL!!! should be 0

                }

                public static int recursiveCompare(String s1, String S2){
                        if  (s1.length() ==0 || s2.length()==0){
                                if ((s1.length() ==0 && s2.length()==0)){result = 0;}
                                else if ((s1.length() !=0 || s2.length()==0)){result =  1;}
                                else if ((s1.length() ==0 || s2.length()!=0)){result = -1;}
                        }

                        else 
                        {
                            recursiveCompareHelper(s1, s2,0);
                        }
                return result;
                }

            public static int recursiveCompareHelper(String s1,String s2, int index){

                    try{

                        if (s1.regionMatches(true,index,s2,index,1)){
                                result = recursiveCompareHelper(s1,s2,(index+1));}

                            else {
                                    if (s1.charAt(index) > s2.charAt(index)){
                                        result =1;
                                    }

                                    else if (s1.charAt(index) < s2.charAt(index)){
                                        result =-1;
                                    }

                                    else if (s1.charAt(index) == s2.charAt(index)){ 
                                        result = recursiveCompareHelper(s1,s2,(index+1));
                                    }
                                }

                        } catch (StringIndexOutOfBoundsException e){
                                if      (s1.charAt(index)==0 && s2.charAt(index)== 0){result = 0;}
                                else if (s1.charAt(index)==0 && s2.charAt(index)!= 0){result = 1;}
                                else if (s1.charAt(index)!=0 && s2.charAt(index)== 0){result =-1;}
                        }

                        return result;
            }
        }

Upvotes: 0

Views: 2920

Answers (3)

Nutan
Nutan

Reputation: 786

Main mistake that you have done in your program is in function recursiveCompare you have taken argument as S2 and in a function using variable s2 which is declared as static variable so your function is failing to give correct result. Remember java is case sensitive language and in that case S2 is not same as s2.

below is the program which i have modified use that for your understanding.

    public class Test{

 /*    public static String s1 = "alpha";
        public static String s2 = "delta";
        public static String s3 = "omega";
        public static String s4 = "alpha";*/
        public static int  result;


        public static void main (String[]args){

              String s1 = "alpha";
              String s2 = "delta";
              String s3 = "omega";
              String s4 = "alpha";

             System.out.println(recursiveCompare(s1,s2));  // -1  good
             System.out.println(recursiveCompare(s3,s1));  //  1  good
            System.out.println(recursiveCompare(s4,s1));  //  1  FAIL!!! should be 0
            System.out.println(recursiveCompare(s2,s3));  // -1  good
            System.out.println(recursiveCompare(s1,s1));  // -1  FAIL!!! should be 0

            }



            public static int recursiveCompare(String s1, String S2){ 
                    if  (s1.length() ==0 || S2.length()==0){ // here you have to use S2 and not s1
                            if ((s1.length() ==0 && S2.length()==0)){result = 0;}
                            else if ((s1.length() !=0 || S2.length()==0)){result =  1;}
                            else if ((s1.length() ==0 || S2.length()!=0)){result = -1;}
                    }

                    else 
                    {
                        recursiveCompareHelper(s1, S2,0);
                    }
            return result;
            }



        public static int recursiveCompareHelper(String s1,String s2, int index){


                             // System.out.println("String are" + s1+"   "+ s2 + " index is "+ index);

                              if(index<s1.length()) {

                                 // System.out.println("Characters at  index : "+ s1.charAt(index)+ "  "+ s2.charAt(index));

                                if (s1.charAt(index) > s2.charAt(index)){
                                    //System.out.println("In the if condition");
                                    result= 1;
                                }

                                else if (s1.charAt(index) < s2.charAt(index)){
                                    //System.out.println("In the else if condition");
                                    result =-1;
                                }

                                else if (s1.charAt(index) == s2.charAt(index)){
                                    //System.out.println("Character at "+index);
                                    result = recursiveCompareHelper(s1,s2,(index+1));
                                }
                              }
                              else return 0;

                            return result;



        }
    } 

Upvotes: 0

Omer Vishlitzky
Omer Vishlitzky

Reputation: 76

first of all, notice you pass S2 as a parameter to recursiveCompare, not s2, so actually you compare everything with "delta" because s2 is a static variable. second of all, when comparing strings, as soon as you find a difference you can return an answer, its wrong just to change the value of result because it can be changed again later and return a wrong answer.

this is my solution, inside each recursive call I compare between the first letters and if they're equal, I call the function recursively without the first letters of the strings

public class test {

    public static String s1 = "alpha";
    public static String s2 = "delta";
    public static String s3 = "omega";
    public static String s4 = "alpha";

    public static void main(String[] args) {

        System.out.println(recursiveCompare(s1, s2));  // -1  good
        System.out.println(recursiveCompare(s3, s1));  //  1  good
        System.out.println(recursiveCompare(s4, s1));  //  1  FAIL!!! should be 0
        System.out.println(recursiveCompare(s2, s3));  // -1  good
        System.out.println(recursiveCompare(s1, s1));  // -1  FAIL!!! should be 0

    }

    public static int recursiveCompare(String s1, String s2) {
        if (s1.length() == 0 || s2.length() == 0) {
            if ((s1.length() == 0 && s2.length() == 0)) {
                return 0;
            } else if (s1.length() != 0) {
                return 1;
            } else {
                return -1;
            }
        }
        if (s1.charAt(0) < s2.charAt(0)) {
            return -1;
        } else if (s1.charAt(0) > s2.charAt(0)) {
            return 1;
        } else if (s1.charAt(0) == s2.charAt(0)) {
            return 0;
        } else {
            return recursiveCompare(s1.substring(1), s2.substring(1));
        }
    }

}

output:

-1
1
0
-1
0

Upvotes: 2

Nikita A.
Nikita A.

Reputation: 33

You do not need to use the .langth() method. To compare strings, you need to use .equals()

public static int recursiveCompare(String s1, String s2){
    if  (s1.equals(s2)) {
        return 0;
    }

    else
    {
        recursiveCompareHelper(s1, s2,0);
    }
    return result;
}

And in recursiveCompare(String s1, String S2) you hava S2 insted of s2.

Upvotes: 0

Related Questions