C語言中比較字串大小
阿新 • • 發佈:2019-02-09
使用指標比較陣列大小,編譯器採用的是Code:Block。程式如下:
#include <iostream>
using namespace std; int main ()
{
char table1[100], table2[100];
char *s1, *s2;
s1 = table1;
s2 = table2;
int flag = 0;
cin >> s1 >> s2;
for(;*s1!='\0' & *s2!='\0'; s1++,s2++)
{
if(*s1 == *s2)
continue;
if(*s1 > *s2)
{
flag = 1;
break;
}
if(*s1 <*s2)
{
flag = -1;
break;
}
}
switch (flag)
{
case -1:cout << "s1 < s2";break;
case 0 :cout << "s1 = s2";break;
case 1 :cout << "s1 > s2";break;
}
return 0;
}
using namespace std; int main ()
{
char table1[100], table2[100];
char *s1, *s2;
s1 = table1;
s2 = table2;
int flag = 0;
cin >> s1 >> s2;
for(;*s1!='\0' & *s2!='\0'; s1++,s2++)
{
if(*s1 == *s2)
continue;
if(*s1 > *s2)
{
flag = 1;
break;
}
if(*s1 <*s2)
{
flag = -1;
break;
}
}
switch (flag)
{
case -1:cout << "s1 < s2";break;
case 0 :cout << "s1 = s2";break;
case 1 :cout << "s1 > s2";break;
}
return 0;
}