简体   繁体   English

C上的Strcmp()函数实现

[英]Strcmp() function realization on C

I need to make an strcmp function by myself, using operations with pointers. 我需要自己创建一个strcmp函数,使用指针操作。 That's 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 tutor told me that there are small mistake in my code. 但我的导师告诉我,我的代码中存在小错误。 I spend really lot of time making tests, but couldn't find it. 我花了很多时间进行测试,但找不到它。

Does this answer the question of what you're 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'll propose a quick fix : 建议快速解决一下

Change 更改

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

To

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

And why you were wrong: 为什么你错了:

The return values of strcmp() should be: strcmp()的返回值应为:

if Return value < 0 then it indicates str1 is less than str2. 如果返回值<0则表示str1小于str2。

if Return value > 0 then it indicates str2 is less than str1. 如果返回值> 0则表示str2小于str1。

if Return value = 0 then it indicates str1 is equal to str2. 如果返回值= 0则表示str1等于str2。

And you were doing exactly the opposite. 而你正好相反。

@yLaguardia well answered the order problem. @yLaguardia很好地回答了订单问题。

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

The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2 . strcmp函数返回一个大于,等于或小于零的整数,因为s1指向的字符串大于,等于或小于s2指向的字符串。 C11dr §7.24.4.2 3 C11dr§7.24.4.23


Using uint8_t is fine for the vast majority of cases. 对于绝大多数情况,使用uint8_t都可以。 Rare machines do not use 8-bit char , so uint8_t is not available. 稀有机器不使用8位char ,因此uint8_t不可用。 In any case, it is not needed as unsigned char handles the required unsigned compare. 在任何情况下,都不需要它,因为unsigned char处理所需的无符号比较。 (See below about unsigned compare.) (见下面关于无符号比较。)

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

Even higher portable code would use the following to handle when char range and unsigned range match as well as all other char, unsigned char, int, unsigned sizes/ranges. 更高的可移植代码将使用以下内容来处理char范围和unsigned范围匹配以及所有其他char, unsigned char, int, unsigned大小/范围。

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

strcmp() is defined as treating each character as unsigned char , regardless if char is signed or unsigned. strcmp()定义为将每个字符视为unsigned char ,无论char是有符号还是无符号。

... each character shall be interpreted as if it had the type unsigned char ... C11 §7.24.1 3 ......每个字符都应被解释为具有unsigned char的类型...C11§7.24.13


Should the char be ASCII or not is not relevant to the coding of strcmp() . char为ASCII与strcmp()的编码无关。 Of course under different character encoding, different results may occur. 当然,在不同的字符编码下,可能会出现不同的结果。 Example: strcmp("A", "a") may result in a positive answer (seldom used EBCDIC ) with one encoding, but negative ( ASCII ) on another. 示例: strcmp("A", "a")可能会导致一个编码的肯定答案(很少使用EBCDIC ),而另一个编码会产生负( ASCII )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM