當前位置:編程學習大全網 - 編程軟體 - 數組編程:將2個字符串s1和s2比較。若s1>s2輸出1;若s1=s2,輸出0;若s1<s2,輸出-1(不能用strcmp函數)

數組編程:將2個字符串s1和s2比較。若s1>s2輸出1;若s1=s2,輸出0;若s1<s2,輸出-1(不能用strcmp函數)

#include<iostream.h>

#include<string.h>

int min(int a,int b)

{

return ((a<b)?a:b);

}

void main()

{

char a[1000],b[1000];

int lena,lenb,i,t,res,flag=0;

cin>>a>>b;

lena=strlen(a);

lenb=strlen(b);

t=min(lena,lenb);

for(i=0;i<t;i++)

if (a[i]>b[i])

{

res=1;

flag=1;

break;

}

else if (a[i]<b[i])

{

res=-1;

flag=1;

break;

}

if (!flag)

if (lena>lenb)

res=1;

else if (lena<lenb)

res=-1;

else

res=0;

cout<<res<<endl;

}

  • 上一篇:硬件編程適合多大年齡的孩子?
  • 下一篇:找出電腦鍵盤的按鍵分布
  • copyright 2024編程學習大全網