17週目アイテム2--2つの文字列を比較(配列名でパラメータ)

1265 ワード

#include <iostream>
/*
 * Copyright (c) 2013,          
 * All rights reserved.
 *       :     
 *     :2013    12  24 
 *      :v1.0
 *     :       
 *   :     
*/using namespace std;
int astrcmp(const char str1[],const char str2[]);
int main()
{
    int m;
    char s1[]="Hello world.";
    char s2[]="Good morning,mother.";
    m=astrcmp(s1,s2);
    switch(m)
    {
        case 1:cout<<s1<<"  >  "<<s2;break;
        case -1:cout<<s1<<"  <  "<<s2;break;
        case 0:cout<<s1<<"  =  "<<s2;break;
        default:cout<<"error";
    }

    return 0;
}
int astrcmp(const char str1[],const char str2[])
{
    int m;
    int i,j;
    for(i=0,j=0; str1[i]!=0||str2[j]!=0; ++i,++j)
    {
        if(str1[i]!=str2[j])
        {
            if(str1[i]>str2[j]) m=1;
            else if(str1[i]<str2[j])m=-1;
        }
        else if (str1[i]==str2[j])m=0;
    }
    return m;
}

実行結果: