0

Basically, the assignment is being an ass and doesn't want us to use an existing method to compare two strings alphabetically.

It should return 1 if the first string is "bigger" alphabetically than the second (in the sense that 'g' is bigger than 'a'), -1 if the second is bigger, or 0 if they are the same.

Lets say i have

String a = "Cows";
String b = "Horses";

The method should return -1.

My understanding is to use a for() loop to scan both a and b, using charAt(), but I have no idea how to implement this...

EDIT***

reading the answers I've come up with this.

    int compared = 0;
    for (int i = 0; i<s1.length() && i<s2.length(); i++){
        int a = s1.charAt(i);
        int b = s2.charAt(i);
        if(a < b){
            compared = -1;
        }
        else if(a > b){
            compared = 1;
        }
    }
    return compared;

The strings being compared all start with uppercase, so it shouldn't be a problem. However, when using the normal String.compareTo() and a bubblesort method that counts the number of times this method was called while sorting a predertermined string array, I get different results, which means something is obviously wrong.

For people viewing this and having the same problem, here's how the code works

    int compared = 0;
    //program assumes strings are equal

    for (int i = 0; i<s1.length() && i<s2.length(); i++){
        //for() loop goes on until the largest string
        int a = s1.charAt(i);
        int b = s2.charAt(i);
        //convert char into int for comparison just in case
        if(a < b){
            compared = -1;
            break;
            //breaks at the first occurence of non equal characters
        }
        else if(a > b){
            compared = 1;
            break;
            //same as above
        }
    }
    return compared;
1
  • 2
    Sounds like you do have an idea. That for loop and charAt thing. Try that. Commented Oct 26, 2014 at 21:50

3 Answers 3

1

Without really giving you the answer, given this is homework, I think I can point you in to the right direction.

Your understanding to use a for loop to iterate through the two words is correct, as well as using the String.charAt. Now what I want you to start with is to find the end of your for loop - that is, find out how many characters you should iterate through?

A hint:

'b' > 'a'

This hint has a trap in it though. What should you do, given the following strings?

Haskell

java

A hint for this too:

'A' < 'a'

Sign up to request clarification or add additional context in comments.

8 Comments

arguments for my for loop are so: for (int i = 0; i< s1.length() && i<s2.length(); i++ ), where s1 is the first string and s2 is the second. The for loop should go on until any of the strings are done.
That is correct. Now, compare each character of both words one at a time :)
forgot to add case should be insensitive,but i cannot use toUpperCase()/toLowerCase()
I see. In that case you should probably know that 'a' - 32 = 'A'. And of course then 'A' + 32 = 'a' holds true as well. Refer to here: asciitable.com
edited the OP , method seems to work, but does not give the same results as String.compareTo()
|
1

Yes your idea is the right way. Loop through the characters of both Strings while comparing them. To compare cast the character you get as (int) to get the ASCII code, then by comparing the ASCII code of each letter you can decide which one is > < or = .

Comments

0
public  int strCompare(String a,String b)
{
   int i=0;

   while(i<a.length()&&i<b.length()&&(a.charAt(i)==b.charAt(i)))
   {
        i++;          

   }
    if(i==a.length()&&i==b.length())
        return 0;
    if(i>=a.length())
        return -1;
    else if(i>=b.length())
        return 1;
return a.charAt(i)-b.charAt(i);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.