I wrote a function that replaces the function strcmp()
.
The cases are:
1) strings are the same
2) second string will come first in the dictionary.
3) first string will come first in the dictionary.
In theory:
'a' > 'b'
So 'a' is the first string to come in the dictionary, however, my code doesn't exactly view it like this, instead it treats it like it's case 1.
Here is my code:
int cmp(char fString[], char sString[])
{
int flag = 0;
int i = 0;
for (i = 0; fString[i]; i++) {
if (fString[i] == sString[i]) {
flag = 0;
} else
if (fString[i] > sString[i]) {
flag = 1;
} else {
flag = -1;
}
}
return flag;
}
The conditions are:
if (cmp(fString, sString) == 0) {
printf("Strings are the same.\n");
} else
if (cmp(fString, sString) > 0) {
printf("First string will come first in the dictionary\n");
} else {
printf("Second string will come first in the dictionary\n");
}
Where did I do wrong?