用c语言编写一个程序,从键盘上输入3个字符串,输出其中的最大者

如题所述

1. int strcmp( const char *str1, const char *str2 );

功能:比较字符串str1 and str2, 返回值如下:

返回值

< 0    str1 < str2

= 0    str1 == str2

> 0    str1 > str2

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100], b[100], c[100];
    printf("input 3 string :\n");
    gets(a);
    gets(b);
    gets(c);
    char* p = strcmp(a, b) >= 0 ? a : b;
    printf("greater string :%s\n", strcmp(p, c) >= 0 ? p : c);
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-15
#include <stdio.h>
#include <string.h>

#define LONGTH 10 //定义字符串最大长度
void main()
{
char a[LONGTH],b[LONGTH],c[LONGTH];
char *max;
printf("请输入三个字符串,以空格隔开:");
scanf("%s %s %s",a,b,c);
printf("输入的三个字符串为:\n");
printf("a=%s\n",a);
printf("b=%s\n",b);
printf("c=%s\n",c);
max=a;
if(strcmp(max,b)<0) max=b;
if(strcmp(max,b)<0) max=c;
printf("\nmax=%s\n",max);
}
我以前写的,可以运行,希望有帮助本回答被提问者采纳
第2个回答  2011-05-15
#include "stdio.h"
void main()
{int a,b,c,max;
printf("请输入三个数字:");
scanf("%d%d%d",&a,&b,&c);
max=a;
if(max<b) max=b;
if(max<c)max=c;
printf("max=%d\n",max);
}
第3个回答  2011-05-15
一楼的稍微修改一下就行了。
#include <stdio.h>
#include <string.h>
#define MAXLEN 1024
#define NUM 3
int main(int argc,char **argv)
{
char arr[NUM][MAXLEN];
char maxChar[MAXLEN];
memset(maxChar,0,MAXLEN);
int iCount=0;
for (iCount=0;iCount<NUM;iCount++)
memset(arr[iCount],0,MAXLEN);
for (iCount=0;iCount<NUM;iCount++)
{
printf("Input strings:");
scanf("%s",arr[iCount]);
}
strcpy(maxChar,arr[0]);
for (iCount=0;iCount<NUM;iCount++)
{
if(strcmp(maxChar,arr[iCount])<0) strcpy(maxChar,arr[iCount]);
}
printf("%s\n",maxChar);
return 0;
}

C:\>gcc -g test.c -o test

C:\>test
Input strings:abcd
Input strings:acdd
Input strings:btest
btest

C:\>
相似回答