Strcmp () function implementation in C

I need to create the strcmp function myself using pointer operations. Here is what I got:

int mystrcmp(const char *str1, const char *str2) { while ('\0' != *str1 && *str1 == *str2) { str1 += 1; str2++; } int result1 = (uint8_t)(*str2) - (uint8_t)(*str1); // I need (uint8_t) to use it with Russian symbols. return result1; } 

But my teacher told me that there is a small error in my code. I spend a lot of time doing tests, but cannot find it.

+7
c
source share
2 answers

Does this answer the question of what you are doing wrong?

 #include <stdio.h> #include <stdint.h> #include <string.h> int mystrcmp(const char *str1, const char *str2); int main(void) { char* javascript = "JavaScript"; char* java = "Java"; printf("%d\n", mystrcmp(javascript, java)); printf("%d\n", strcmp(javascript, java)); return 0; } int mystrcmp(const char *str1, const char *str2) { while ('\0' != *str1 && *str1 == *str2) { str1 += 1; str2++; } int result1 = (uint8_t)(*str2) - (uint8_t)(*str1); // I need (uint8_t) to use it with Russian symbols. return result1; } 

Output:

 -83 83 

I suggest a quick fix :

Change

 int result1 = (uint8_t)(*str2) - (uint8_t)(*str1); 

To

 int result1 = (uint8_t)(*str1) - (uint8_t)(*str2); 

And why were you wrong:

The return values ​​of strcmp() should be:

if the return value is <0, then indicates that str1 is less than str2.

if the return value is> 0, then it indicates that str2 is less than str1.

if the return value = 0, then it indicates that str1 is equal to str2.

And you did exactly the opposite.

+9
source share

@yLaguardia answered the order problem well.

 int strcmp(const char *s1, const char *s2); 

The strcmp function returns an integer greater than, equal to, or less than zero, respectively, since the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2 , C11dr Β§7.24.4.2 3


Using uint8_t great for the vast majority of cases. Rare machines do not use 8-bit char , so uint8_t not available. In any case, this is not necessary, since unsigned char handles the required unsigned char comparison. (See below for unsigned comparisons.)

 int result1 = ((unsigned char)*str1 - (unsigned char)*str2); 

Even higher portable code will use the following to handle the range of char and unsigned , as well as all other sizes and ranges of char, unsigned char, int, unsigned .

 int result1 = ((unsigned char)*str1 > (unsigned char)*str2) - ((unsigned char)*str1 < (unsigned char)*str2); 

strcmp() defined as processing each character as an unsigned char , regardless of whether the char signed or unsigned.

... each character should be interpreted as if it were of type unsigned char ... C11 Β§7.24.1 3


If char is ASCII or not, this does not apply to strcmp() encoding. Of course, with different character encodings, different results may occur. Example: strcmp("A", "a") can lead to a positive answer (rarely used by EBCDIC ) with one encoding, but a negative ( ASCII ) to another.

+5
source share

All Articles