程序填空题:比较字符串的大小
输入2个字符串,比较它们的大小。要求定义和调用函数cmp(s, t),该函数逐个比较字符串s和t中的对应字符,直到对应字符不等或比较到串尾。若s和t相等则返回0,若不相等则返回不相等字符的差值,即若s大于t则返回一个正数,若s小于t则返回一个负数。输入输出示例如下:
输入:
```c++
4324erfda 4324etgggds
```
输出:
```c++
"4324erfda" < "4324etgggds"
```
```c++
# include
#define MAXS 80
int cmp ( char *s, char *t );
int main( )
{
char s[MAXS], t[MAXS];
scanf ( "%s%s", s, t );
if ( cmp(s, t) > 0)
printf("\"%s\" > \"%s\"\n", s, t);
else if ( cmp(s, t) == 0)
printf("\"%s\" = \"%s\"\n", s, t);
else
printf("\"%s\" < \"%s\"\n", s, t);
}
int cmp ( char *s, char *t )
{
while (@@[*s != '\0'](1) ){
if ( *s != *t ) break;
@@[s++; t++](1);
}
return @@[*s - *t](1);
}
```
答案:
第1空:*s != '\0'
第2空:s++; t++
第3空:*s - *t
输入:
```c++
4324erfda 4324etgggds
```
输出:
```c++
"4324erfda" < "4324etgggds"
```
```c++
# include
#define MAXS 80
int cmp ( char *s, char *t );
int main( )
{
char s[MAXS], t[MAXS];
scanf ( "%s%s", s, t );
if ( cmp(s, t) > 0)
printf("\"%s\" > \"%s\"\n", s, t);
else if ( cmp(s, t) == 0)
printf("\"%s\" = \"%s\"\n", s, t);
else
printf("\"%s\" < \"%s\"\n", s, t);
}
int cmp ( char *s, char *t )
{
while (@@[*s != '\0'](1) ){
if ( *s != *t ) break;
@@[s++; t++](1);
}
return @@[*s - *t](1);
}
```
答案:
第1空:*s != '\0'
第2空:s++; t++
第3空:*s - *t