简体   繁体   中英

Compares two strings lexicographically using charAt() and without using compareTo() method

if the first string is lexicographically greater than the second string it should return 1,if equal return 0,else -1.It is return 1,-1,0 correctly for some cases,but for this str1 and str2 the return is coming out to be the opposite of the desired output.

public class StringCompare {

    static String testcase1 = "helloworld";
    static String testcase2 = "hellojavaworld";

    public static void main(String args[]) {
        StringCompare testInstance = new StringCompare();
        int result = testInstance.newCompare(testcase1, testcase2);
        System.out.println("Result : " + result);
    }

    // write your code here
    public int newCompare(String str1, String str2) {

        int l1 = str1.length();
        int l2 = str2.length();
        int max = 0;
        if (l1 <= l2) {
            max = l1;
        }
        else
            max = l2;
        int count = 0;

        for (int i = 0; i < max; i++) {
            char ch1 = str1.charAt(i);
            char ch2 = str2.charAt(i);

            if (str2.charAt(i) > str1.charAt(i)) {
                return - 1;
            }

            if (str1.charAt(i) > str2.charAt(i)) {
                return 1;

            }
            if (l1 == l2) {
                if (ch1 == ch2) {
                    count++;
                }
                if (count == max) {
                    return 0;
                }
            }

        }
        if (l1 == l2) return 0;
        if (l1 > l2)
            return 1;
        else
            return - 1;

    }

}

Here's a simplified answer

public class TestStrings {

    public static void main(String[] args) {

        System.out.println(compare("Mike", "Mike"));    // returns 0
        System.out.println(compare("Mikee", "Mike"));   // returns 1
        System.out.println(compare("Mike", "Mikee"));   // returns -1
    }

    public static int compare(String s1, String s2) {
        for (int i = 0; i < Math.min(s1.length(), s2.length()); i++) {
            char c1 = s1.charAt(i);
            char c2 = s2.charAt(i);

            if (c1 > c2) {
                return 1;
            } else if (c2 > c1) {
                return -1;
            }
        }

        if (s2.length() > s1.length()) {
            return -1;
        } else if (s1.length() > s2.length()){
            return 1;
        } else {
            return 0;
        }
    }
}

I used a loop, with the stopping condition as the length of the shortest word. If the words are equal after the length of the shortest word, then the longer word is automatically larger. That's what the if statement at the bottom is for.

you can try:

public class StringCompare {
    static String testcase1 = "helloworld";
    static String testcase2 = "hellojavaworld";

    public static void main(String args[]){
        StringCompare testInstance = new StringCompare();
        int result = testInstance.newCompare(testcase1,testcase2);
        System.out.println("Result : "+result);
    }

    //write your code here
    public int newCompare(String str1, String str2){
        int l1=str1.length();
        int l2=str2.length();
        int max=0;
        if(l1<=l2)
        {
            max =l1;
        }
        else
        max=l2;
        int count=0;


        for (int i =0;i<max;i++) {
            char ch1=str1.charAt(i);
            char ch2=str2.charAt(i);

            if(str2.charAt(i)>str1.charAt(i))
            {
                return -1;
            }
            if(str1.charAt(i)>str2.charAt(i))
            {
                return 1;
            }
        }
        if(l1==l2)
        {
            return 0;
        }else if (l1 < l2){
            return -1;
        }else{
            return 1;
        }

     }                

In this case where String testcase1 = "helloworld" and String testcase2= "hellojavaworld" your for loop will run from char 'h' to char 'o'(i=0 to i=4) and none of the if conditions inside your for loop will be satisfied and as soon as i gets incremented to 5

 //str1.charAt(i)='w' and str2.charAt(i)=j
 if(str1.charAt(i)>str2.charAt(i))   //w(ASCII=119) > j(ASCII=106)
 {
        return 1;                    //return 1 and control return to the calling function

  }

So result=1 . Or in short your code is working properly. You can specify what you want the output to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM