| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5995 人关注过本帖
标题:输出字符串,求单词个数。求大神!
只看楼主 加入收藏
zhb151017212
Rank: 1
等 级:新手上路
帖 子:41
专家分:6
注 册:2012-10-15
结帖率:73.33%
收藏
已结贴  问题点数:10 回复次数:9 
输出字符串,求单词个数。求大神!
//输入一行字符串,统计其中多少个单词,单词之间用空格分开。。。
#include<stdio.h>
#include<string.h>
#define N 100
int main()
{
    char str[N];
    char c;
    int word=0,num=0,i;
    gets(str);
    for(i=0;c=(str[N])!='\0';i++)
        if(c==' ')word=0;
            //printf("单词数为%d",num);
        else if(c!=' ')
        {
            word=1;
            num++;
        }
        printf("单词位数为%d\n",num);
        return 0;
}
搜索更多相关主题的帖子: return word include 字符串 单词 
2012-10-22 21:02
雪狼MJ
Rank: 8Rank: 8
来 自:甘肃
等 级:蝙蝠侠
威 望:4
帖 子:267
专家分:853
注 册:2012-5-27
收藏
得分:2 
for循环的条件判断有问题,我不知道你是想求单词的个数还是的个数,就都统计了,代码如下:
#include<stdio.h>
#include<string.h>
#define N 100
int main(void)
{
    char str[N];
    char c;
    int word=1,num=0,i;
    gets(str);
   
    for(i=0;str[i]!='\0';i++)
    {   
        if(str[i]==' ' && (str[i+1]<='z' && str[i+1]>='A')) word++;
        
        else if(str[i]<='z' && str[i]>='A')
        {
            num++;
        }
    }
        
    printf("单词数为%d, 字母数为%d\nf",word, num);
    return 0;
}

Edsger Dijkstra:算法+数据结构=程序
2012-10-22 21:37
SamWoo
Rank: 2
等 级:论坛游民
帖 子:13
专家分:48
注 册:2012-10-17
收藏
得分:0 
#include<stdio.h>
#include<string.h>
#define N 100
int main(void)
{
    char str[N];
    int word=0,num=0,other=0,space=0;
    int i;
    gets(str);
    for(i=0;str[i]!='\0';i++)
    {   
        if(str[i]<='Z' && str[i]>='A'||str[i]<='z' && str[i]>='a')
           { word++;}
        else if(str[i]>='0'&&str[i]<='9')
           { num++;}
        else if(str[i]==' ')
           {space++;}
        else
           { other++;}
    }   
    printf("单词数为%d, 数字数为%d,空格数为%d,其他符号数为%d\n",word, num,space,other);
    return 0;
}
2012-10-23 01:05
zhb151017212
Rank: 1
等 级:新手上路
帖 子:41
专家分:6
注 册:2012-10-15
收藏
得分:0 
回复 2楼 雪狼MJ
谢谢。我求的是单词数。。。
2012-10-23 19:47
zhb151017212
Rank: 1
等 级:新手上路
帖 子:41
专家分:6
注 册:2012-10-15
收藏
得分:0 
回复 2楼 雪狼MJ
如果只输入空格好像就错了。。。
2012-10-23 19:50
SamWoo
Rank: 2
等 级:论坛游民
帖 子:13
专家分:48
注 册:2012-10-17
收藏
得分:2 
#include<stdio.h>
#include<string.h>
#define N 100

int main(void)
{
    char str[N];
    int i,word=0,num=0;
    char c;
   
    gets(str);
    for(i=0;(c=str[i])!='\0';i++)
        if(c==' ') word=0;
        else if(word==0)
        {
            word=1;
            num++;
        }
    printf("There are %d words in the line.\n",num);
    return(0);
}
2012-10-23 22:09
cjbs10086
Rank: 2
等 级:论坛游民
帖 子:32
专家分:45
注 册:2012-10-13
收藏
得分:2 
#include<stdio.h>
int main()
{
    char str[1001];
    while(gets(str))
    {
        int i,flag=1,num=0;
        for(i=0;str[i]!='\0';i++)
        {
            if(str[i]!=' ')
            {
                if(flag)
                {
                    flag=0;
                    num++;
                }
            }
            else
                flag=1;
        }
        printf("%d\n",num);
    }
    return 0;
}
2012-10-23 22:30
C_戴忠意
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:575
专家分:1349
注 册:2011-10-21
收藏
得分:2 
程序代码:
#include <stdio.h>
#include <string.h>

#define N 10000

int main()
{
    char str[N];
    while(gets(str))
    {   
        //puts(str);
        int word_num=0;
        int len=strlen(str);
        for(int i=0;i<len;i++)
            if(str[i]==' ')    continue ;//去掉前导空格
            else break ;
        for(int j=len-1;j>=0;j--)
            if(str[j]==' ') continue ;//去掉后导空格
            else break ;
        for(;i<j;i++)
        {
            if((str[i]==' ')&&(str[i+1]!=' '))    word_num++;
        }
        printf("%d\n",word_num+1);//因为最后一个单词一定判断不成功,所以直觉加1
        getchar();
    }   
    return 0;
}
测试数据:
a b c d e f
6
  a b c d e f
6
 a  b  c d      e    f
6
     a    b   c   d   e          f
6


[ 本帖最后由 C_戴忠意 于 2012-10-24 08:49 编辑 ]

编程之路定要走完……
2012-10-23 22:56
青春无限
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江苏
等 级:贵宾
威 望:24
帖 子:3451
专家分:19340
注 册:2012-3-31
收藏
得分:2 
										

学 会看代码…学习写程序…学会搞开发…我的目标!呵呵是不是说大话啊!!一切皆可能
2012-10-23 23:03
zhb151017212
Rank: 1
等 级:新手上路
帖 子:41
专家分:6
注 册:2012-10-15
收藏
得分:0 
回复 8楼 C_戴忠意
如果我输入汉字的话,它也把它当做字符判断了,当然我知道汉子也是字符串。。。
不过依然要谢谢你。。。
2012-10-25 21:10
快速回复:输出字符串,求单词个数。求大神!
数据加载中...
 
   



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

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