Basically i have an array of strings and i need to check if they are arranged in lexographic order (case insensitive of upper or lower case, meaning it doesn't matter which one it is). For that I took the already created strcmp function and re-arranged it so that it won't matter if it's upper or lower case. But when entering str_compare, it won't enter the while loop and just exits the function. Does anyone know why? Here is the code:
int main(){
...
char * banned_words[N];
...
if (!are_sorted(banned_words, n)) {
printf("Words are not sorted correctly!\n");
free_strings(banned_words, n);
return ERROR;
}
...
}
bool are_sorted(char * strings[], int n) {
int length = n;
int result = 0;
for (int i = 0; i < length - 1; i++) {
result += str_compare(strings[i], strings[i + 1]);
}
if (result > 0)
return false;
else
return true;
}
int str_compare(char **str1, char **str2){
while (str1 && str2) {
if (str1 >= 'A' && str1 <='Z' && (str1 + 'a' - 'A') == str2){
**str1++;
**str2++;}
else if (str2 >= 'A' && str2<='Z' && (str2 + 'a' - 'A') == str1){
**str1++;
**str2++;}
else if (str1 == *str2){
**str1++;
**str2++;}
else break;
}
if (str1 >= 'A' && str1<='Z')
return (str1 +'a' - 'A') - str2;
else if (str2 >= 'A' && str2<='Z')
return str1 - (str2 +'a'-'A');
else return str1 - str2;
}