【字符串的运算】
回忆一下从三个整数中输出最大的整数的程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h> int main(int argc, char *argv[]) { int a, b, c, max; scanf("%d%d%d", &a, &b, &c); max = a; if (b>max) max = b; if (c>max) max = c; printf("%d\n", max); return 0; } |
根据字符串函数的用法,从三个字符串中输出最大的字符串的程序如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char a[80], b[80], c[80], max[80]; gets(a); gets(b); gets(c); strcpy(max, a); if (strcmp(b,max)>0) strcpy(max, b); if (strcmp(c,max)>0) strcpy(max, c); puts(max); return 0; } |