-2

I want to Implement strcmp so this is what i have try:

int compare(char *str1, char *str2, int maximumCharactersToCompare)
{
    int i;
    int result = 0;
    for (i = 0; i < maximumCharactersToCompare; i++)
    {
        if (str1[i] > str2[i])
            result = 1;
        else if (str1[i] < str2[i])
            result = -1;
        if (str1[i] == '\0' || str1[i] == '\0')
            break;
    }

    return result;
}

My usage:

char str1[127];
char str2[127];

printf("Please enter first string: ");
scanf("%s", str1);

printf("Please enter second string: ");
scanf("%s", str2);

int result = compare(str1, str2, 127);

So my problem is that if i try to compare "test" and "test " this return 0 and i can see that str2 is equat to "test" instead of "test " and thats why my result is 0

user979033
  • 5,430
  • 7
  • 32
  • 50

1 Answers1

0

you type

if (str1[i] == '\0' || str1[i] == '\0')

try

if (str1[i] == '\0' || str2[i] == '\0')
Cesar Augusto
  • 268
  • 4
  • 16