| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1539 人关注过本帖
标题:只是课后题,可是错在哪了?
只看楼主 加入收藏
shinobifc
Rank: 2
等 级:论坛游民
帖 子:12
专家分:20
注 册:2008-7-31
结帖率:100%
收藏
 问题点数:0 回复次数:17 
只是课后题,可是错在哪了?
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。我的代码如下:

main()
{
    char c[100];
    printf("Please input a string less than 100 words:");
    scanf("%s",c);
    int i, alphabet=0, number=0, space=0, others=0;
    for ( i=0; i<100; i++ )
    {
        if (c[i]==32)
            space++;
         else if (c[i]>=48 && c[i]<=57)
            number++;
         else if(c[i]>=65 && c[i]<=90  ||  c[i]>=97 && c[i]<=122)
            alphabet++;
        else others++;
    }
    printf("The string has %d alphabets, %d numbers, % spaces and %d other character.\n", alphabet,number,space,others);
}

一编译,就出现如下错误信息:
expression syntax in function main
菜鸟请高手解决下,谢谢!看在我自学C、自主编的勇气上~~~
搜索更多相关主题的帖子: 其他 英文字母 number others 
2010-01-18 13:00
梁子
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:83
专家分:191
注 册:2009-9-20
收藏
得分:0 
    char c[100];
    int i, alphabet=0, number=0, space=0, others=0;
    printf("Please input a string less than 100 words:");
    scanf("%s",c);
这样试试看看行不。
2010-01-18 13:23
梁子
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:83
专家分:191
注 册:2009-9-20
收藏
得分:0 
printf("The string has %d alphabets, %d numbers, %d spaces and %d other character.\n", alphabet,number,space,others);
楼主好粗心。
2010-01-18 13:26
rock10822
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-1-18
收藏
得分:0 
你不能用scanf("%s",c);来计入输入语句。应为scanf("%s",c);是以空格或回车结束的,即使他最后会计入最后的空格或回车,但你现在也应该知道如果你输入过程中出现了空格或回车 ,程序就误认为你要输入结束了

[ 本帖最后由 rock10822 于 2010-1-18 13:44 编辑 ]
2010-01-18 13:28
梁子
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:83
专家分:191
注 册:2009-9-20
收藏
得分:0 
再发一次贴,vc++下调试通过。
#include<stdio.h>
int main(void)
{
    char c[100];
    int i, alphabet=0, number=0, space=0, others=0;//要先定义,后写运行代码
    printf("Please input a string less than 100 words:");
    gets(c);//用scanf输入字符串时遇空格结束。按楼主的意思,这里应该用gets吧。
    for ( i=0; i<100; i++ )
    {
        if (c[i]==32)
            space++;
         else if (c[i]>=48 && c[i]<=57)
            number++;
         else if(c[i]>=65 && c[i]<=90  ||  c[i]>=97 && c[i]<=122)
            alphabet++;
        else others++;
    }
    printf("The string has %d alphabets, %d numbers, %d spaces and %d other character.\n", alphabet,number,space,others);
    return 0;
}
2010-01-18 13:43
rock10822
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-1-18
收藏
得分:0 
回复 5楼 梁子
你好强哦
2010-01-18 13:45
烈烈水云天
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:56
专家分:33
注 册:2009-12-30
收藏
得分:0 
#include<stdio.h>
int main(void)
{
    char c[100];
    int i, alphabet=0, number=0, space=0, others=0;
    printf("Please input a string less than 100 words:");
    gets(c);
    for ( i=0; i<100; i++ )
    {
        if (c[i]==32)
            space++;
         else if (c[i]>=48 && c[i]<=57)
            number++;
         else if(c[i]>=65 && c[i]<=90  ||  c[i]>=97 && c[i]<=122)
            alphabet++;
        else others++;
    }
    printf("The string has %d alphabets, %d numbers, %d spaces and %d other character.\n", alphabet,number,space,others);
    return 0;
}

爱拼才会赢
2010-01-18 13:55
VV程序员
Rank: 1
等 级:新手上路
帖 子:20
专家分:1
注 册:2009-1-14
收藏
得分:0 
要先定义在写代码。。 c语言有这样严格的要求   LZ要切记哦
2010-01-18 15:25
树上月
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:114
专家分:154
注 册:2010-1-6
收藏
得分:0 
1
# include <stdio.h>
int main(void)
{
    int i,n,digit,letter,other;
    char ch;
    printf("Enter n:");
    scanf("%d",&n);
    printf("Enter %d characters:",n);
    digit=letter=other=0;
    for(i=1;i<=n;i++){
        ch=getchar();
        if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
            letter++;
        else if(ch>='0'&&ch<='9')
            digit++;
        else
            other++;
    }
    printf("letter=%d digit=%d other=%d\n",letter,digit,other);
    return 0;
}
2
# include <stdio.h>
int main(void)
{
    int i,n,digit,blank,other;
    char ch;
    printf("Enter n:");
    scanf("%d",&n);
    digit=blank=other=0;
    printf("Enter %d characters:",n);
    for(i=1;i<=n;i++){
        ch=getchar();
    switch(ch){
        case' ':
        case'\n':
            blank++;
            break;
        case'0':  case'1':  case'2':  case'3':  case'4':
        case'5':  case'6':  case'7':  case'8':  case'9':
            digit++;
            break;
        default:
            other++;
            break;
    }
     }
    printf("blank=%d digit=%d other=%d\n",blank,digit,other);
   
    return 0;
}

每一个不曾起舞的日子,都是对未来的一种辜负......
2010-01-18 17:07
阿89327710
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-2-12
收藏
得分:0 
这个 不可以用 scanf
2010-02-12 12:12
快速回复:只是课后题,可是错在哪了?
数据加载中...
 
   



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

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