Creating my own strcmp () function in C

后端 未结 6 994
故里飘歌
故里飘歌 2020-12-18 15:40

I was assigned by my teacher to write my own strcmp() function in C. I did create my own version of said function, and I was hoping to get some feedback.

<
6条回答
  •  有刺的猬
    2020-12-18 16:11

    strcmp compares alphabetically: so "aaa" < "b" even though "b" is shorter.

    Because of this, you can skip the length check and just do the letter by letter comparison. If you get to a NULL character while both strings are equal so far, then the shorter one is the lesser one.

    Also: make StringsAreEqual == 0, not 1 for compatibility with standard sorting functions.

提交回复
热议问题