| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 408 人关注过本帖
标题:菜鸟.... 不太懂得用字符串
只看楼主 加入收藏
tc121091
Rank: 1
等 级:新手上路
帖 子:6
专家分:2
注 册:2011-1-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
菜鸟.... 不太懂得用字符串
如题, 做题目时出了点问题, 运行程式时会自动停止。

题目要我们找出最大和最小单词, 当用户输入4个字母的单词,程式便停止读入。
例子:
Enter the word: dog
Enter the word: zebra
Enter the word: rabbit
Enter the word: cat
Enter the word: fish

Smallest word: cat
Biggest word: zebra

程序代码:
#include<stdio.h>
#include<string.h>
#define N 100

void main()
{
    char smallest[N]={0L},biggest[N]={0L};
    char input[N];
    int i,length;
    do
    {
        printf("Enter the word: ");
        for (i=0;i<N;i++)
        {
            scanf("%c",&input[i]);
            if (input[i]=='\n')
                break;
        }
        printf("\n\n");
        length=strlen(input);
        if (strcmp(smallest,input)>=0)
            strcpy(smallest,input);
        if (strcmp(biggest,input)<0)
            strcpy(biggest,input);
    }
    while (length!=4);

    printf("\nThe smallest word is %c",smallest);
    printf("\nThe biggest word is %c",biggest);
}

搜索更多相关主题的帖子: 字符串 word 用户 
2011-04-23 02:55
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:15 
程序代码:
#include<stdio.h>
#include<string.h>
#define N 100

void main()
{
    char smallest[N]={0L},biggest[N]={0L};
    char input[N];
    int i,j=0;
    do
    {
        printf("Enter the word: ");
        for (i=0;i<N;i++)
        {
            scanf("%c",&input[i]);
            if (input[i]=='\r')
                break;
        }
       intput[i]='\0';
        printf("\n\n");
        if (strcmp(smallest,input)>=0)
            strcpy(smallest,input);
        if (strcmp(biggest,input)<0)
            strcpy(biggest,input);
        j++;
    }
    while (j!=4);

    printf("\nThe smallest word is %s",smallest);
    printf("\nThe biggest word is %s",biggest);
}


   唯实惟新 至诚致志
2011-04-23 07:41
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:0 
字符串是整行输入吗?
2011-04-23 09:05
Noll_Nie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:71
专家分:139
注 册:2011-4-19
收藏
得分:5 
使用字符串函数就好了,gets()和puts()可以直接操作字符串
2011-04-23 10:14
快速回复:菜鸟.... 不太懂得用字符串
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019450 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved