如何strcmp()的工作? [英] How does strcmp() work?

查看:90
本文介绍了如何strcmp()的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在四处寻找一个答案公平一点。我要做出一系列的我自己的字符串函数如 my_strcmp() my_strcat()等。

确实的strcmp()通过两个字符数组的每个索引工作,如果ASCII值是两个字符串的相同指数越小,该字符串有字母更大因此一个0或1或2,则返回?我想林问是什么,它​​使用的字符的ASCII值返回这些结果?

任何帮助将大大AP preciated。

[修订]

OK,所以我想出这个...它适用于所有的情况下,除了当第二个字符串是大于第一。

任何提示吗?

  INT my_strcmp(字符S1 [],CHAR S2 [])
{
    INT I = 0;
    而(S1 [I]!='\\ 0')
    {
        如果(S2 [I] =='\\ 0'){返回1; }
        否则如果(S1 [1] - ; S2由[i]){返回-1; }
        否则如果(S1 [Ⅰ]≥S2由[i]){返回1; }
        我++;
    }
    返回0;
}
INT主(INT ARGC,CHAR *的argv [])
{
    INT结果= my_strcmp(的argv [1],ARGV [2]);    的printf(价值数:%d \\ n,结果);    返回0;}


解决方案

伪code STRCMP 的执行会去是这样的:

 定义STRCMP(S1,S2):
    P1 = str1中第一个字符的地址
    P2 = STR2赛车的第一个字符的地址    而P1的内容不等于空:
        如果P2为null的内容:
            返回1        如果P2比P1的内容大内容:
            返回-1        如果P1 P2相比内容更加的内容:
            返回1        提前P1
        提前P2    如果P2的内容不等于空:
        返回-1    返回0

这基本上它。每个字符依次进行比较的一个做出关于第一或第二字符串是否是更大,是根据该字符。

只有当人物是相同的,你移动到下一个字符,如果的所有的是相同的,则返回字符为零。

请注意,你可能不一定能得到1和-1,规格说任何积极或消极的价值就足够了,所以你应该经常检查与&LT的返回值; 0 > 0 == 0

至于说成真正的C会相对简单:

  INT myStrCmp(为const char * S1,为const char * S2){
    const的无符号字符* P1 =(const的无符号字符*)S1;
    const的无符号字符* P2 =(const的无符号字符*)S2;    而(* P1!='\\ 0'){
        如果(* P2 =='\\ 0')返回1;
        如果(* P2> * P1)返回-1;
        如果(* P1> * P2)返回1;        P1 ++;
        P2 ++;
    }    如果(* P2 ='\\ 0'!)返回-1;    返回0;
}


也请记住,在人物的背景下大不一定是基于简单的ASCII顺序进行的所有的字符串函数。

C具有潜在的字符集称为区域设置一个概念,它指定(除其他事项外)整理或排序,你会发现,例如,字符 A A A A 都被认为是相同。这将发生像的strcoll 功能。

I've been looking around a fair bit for an answer. I'm going to make a series of my own string functions like my_strcmp(), my_strcat(), etc.

Does strcmp() work through each index of two arrays of characters and if the ASCII value is smaller at an identical index of two strings, that string is there alphabetically greater and therefore a 0 or 1 or 2 is returned? I guess what Im asking is, does it use the ASCII values of characters to return these results?

Any help would be greatly appreciated.

[REVISED]

OK, so I have come up with this... it works for all cases except when the second string is greater than the first.

Any tips?

int my_strcmp(char s1[], char s2[])
{   
    int i = 0;
    while ( s1[i] != '\0' )
    {
        if( s2[i] == '\0' ) { return 1; }
        else if( s1[i] < s2[i] ) { return -1; }
        else if( s1[i] > s2[i] ) { return 1; }
        i++;
    }   
    return 0;
}


int main (int argc, char *argv[])
{
    int result = my_strcmp(argv[1], argv[2]);

    printf("Value: %d \n", result);

    return 0;

}

解决方案

The pseudo-code "implementation" of strcmp would go something like:

define strcmp (s1, s2):
    p1 = address of first character of str1
    p2 = address of first character of str2

    while contents of p1 not equal to null:
        if contents of p2 equal to null: 
            return 1

        if contents of p2 greater than contents of p1:
            return -1

        if contents of p1 greater than contents of p2:
            return 1

        advance p1
        advance p2

    if contents of p2 not equal to null:
        return -1

    return 0

That's basically it. Each character is compared in turn an a decision is made as to whether the first or second string is greater, based on that character.

Only if the characters are identical do you move to the next character and, if all the characters were identical, zero is returned.

Note that you may not necessarily get 1 and -1, the specs say that any positive or negative value will suffice, so you should always check the return value with < 0, > 0 or == 0.

Turning that into real C would be relatively simple:

int myStrCmp (const char *s1, const char *s2) {
    const unsigned char *p1 = (const unsigned char *)s1;
    const unsigned char *p2 = (const unsigned char *)s2;

    while (*p1 != '\0') {
        if (*p2 == '\0') return  1;
        if (*p2 > *p1)   return -1;
        if (*p1 > *p2)   return  1;

        p1++;
        p2++;
    }

    if (*p2 != '\0') return -1;

    return 0;
}


Also keep in mind that "greater" in the context of characters is not necessarily based on simple ASCII ordering for all string functions.

C has a concept called 'locales' which specify (among other things) collation, or ordering of the underlying character set and you may find, for example, that the characters a, á, à and ä are all considered identical. This will happen for functions like strcoll.

这篇关于如何strcmp()的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆