[cpp]
/*
* 程序的版權和版本聲明部分
* Copyright (c)2012, 煙台大學計算機學院學生
* All rightsreserved.
* 文件名稱: zhizhen.cpp
* 作 者:紀子龍
* 完成日期:2012年12月19日
* 版本號: v1.0
* 輸入描述:無
* 問題描述:無
*/
#include<iostream>
using namespace std;
int astrcmp(const char str1[],const char str2[]);//用數組
int main()
{
int i;
char a[50],b[50];
cout<<"請輸入一個字符串"<<endl;
cin>>a>>b;
i=astrcmp(a,b);
if(i==0)
cout<<"第一個字符串等於第二個"<<endl;
else{
if(i>0)
cout<<"第一個字符串大於第二個"<<endl;
if(i<0)
cout<<"第一個字符串小於第二個"<<endl;
}
return 0;
}
int astrcmp(const char str1[],const char str2[])
{
int m,j=0;
for(m=0;str1[m]==str2[m]&&str1[m]!='\0';m++)
j++;
if(str1[j]>str2[j])
return 1;
if(str1[j]<str2[j])
return -1;
if(str1[j]==str2[j])
return 0;
}
#include<iostream>
using namespace std;
int astrcmp(const char *str1,const char *str2);//用指針
int main()
{
int i;
char a[50],b[50];
cout<<"請輸入一個字符串"<<endl;
cin>>a>>b;
i=astrcmp(a,b);
if(i==0)
cout<<"第一個字符串等於第二個"<<endl;
else{
if(i>0)
cout<<"第一個字符串大於第二個"<<endl;
if(i<0)
cout<<"第一個字符串小於第二個"<<endl;
}
return 0;
}
int astrcmp(const char *str1,const char *str2)
{
int m,j=0;
for(m=0;str1[m]==str2[m]&&str1[m]!='\0';m++)
j++;
if(str1[j]>str2[j])
return 1;
if(str1[j]<str2[j])
return -1;
if(str1[j]==str2[j])
return 0;
}
運行結果: