strcmp函数实现

/*
功能:比较字符串s1和s2大小。
一般形式:int strcmp(字符串1,字符串2)
说明:
当s1<s2时,返回-1
当s1=s2时,返回 0
当s1>s2时,返回 1
即两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇''为止。
*/
#include<iostream>
using namespace std;
int strcmp(const char*str1,const char*str2);
int main(void)
{
    int res;
    char * s1="abcdef";
    char * s2="abcdefg";
    res=strcmp(s1,s2);
    cout<<res<<endl;
    getchar();
}
int strcmp(const char *str1,const char *str2)
{
    while(*str1&&*str2&&*str1==*str2)
    {
        str1++;
        str2++;
    }
    if(*str1==*str2)
        return 0;
    else if(*str1>*str2)
        return 1;
    else 
        return -1;
}
原文地址:https://www.cnblogs.com/qianwen/p/3802474.html