码迷,mamicode.com
首页 > 其他好文 > 详细

strcmp 的实现方法

时间:2015-06-06 11:54:41      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <string>
using namespace std;

int mystrcmp(const char * str1,const char * str2)
{
        int i=0;
        while(1)
        {
                if(str1[i]==‘\0‘ &&str2[i]!=‘\0‘)
                {
                        return -1;
                }
                else if(str1[i]!=‘\0‘ &&str2[i] == ‘\0‘)
                {
                        return 1;
                }
                else if(str1[i]==‘\0‘ &&str2[i] == ‘\0‘)
                {
                        return 0;
                }                
                if(str1[i]<str2[i])
                {
                        return -1;
                }
                else if(str1[i]>str2[i])
                {
                        return 1;
                }
                i++;
        }
}

int main()
{
        string str1;
        string str2;
        while(1)
        {
        getline(cin,str1);
        getline(cin,str2);
        cout<<"str1="<<str1<<endl;
        cout<<"str2="<<str2<<endl;
        cout<<mystrcmp(str1.c_str(),str2.c_str())<<endl;
    }
}

strcmp 的实现方法

标签:

原文地址:http://www.cnblogs.com/wystan/p/4556368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!