samedi 29 septembre 2018

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;
        }
    }

Aucun commentaire:

Enregistrer un commentaire