统计字符串中单词的个数

1、单纯统计单词个数,单词与单词之间只考虑空格的情况

// word_statistic.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

#define M 10000
#define N 20

int _tmain(int argc, _TCHAR* argv[])
{
    char str1[M]={0};
    char str2[M][N];
    int     count[M];
    gets(str1);
    cout<<"您输入的字符串是:"<<str1<<endl;
    int len=strlen(str1);        //这里只能使用strlen函数,sizeof函数会输出整个数组的长度  而不是数组中实际的字符个数
    int j=0,i=0,k=0;
    while(j<len)
    {
        while (str1[j]==32)    //这里要用while循环  不能用if语句
            j++;
        while(j<len&&str1[j]!=32)    //j<len很重要  不然统计字符串最后一个单词时while循环会一直执行下去  直到字符串末尾
            str2[i][k++]=str1[j++];
        str2[i][k]='';            //
        count[i]=1;                    //单词计数
        for (int p=0;p<i;p++)            //注意这里for循环的作用域一直持续到k=0;那一句代码
            if (strncmp(str2[p],str2[i],N)==0)        //只比较前N个字符    相等就返回0
            {
                count[p]++;                //注意这里是count[p]++不是count[i]++
                i--;                    //当前单词前面已有重复    所以当前的单词不会计入str2数组中
                break;
            }
            i++;
            k=0;
    }

    //输出结果
    for (int q=0;q<i;q++)
    {
        cout<<str2[q]<<"	"<<count[q]<<endl;
    }
    return 0;
}

上述代码只考虑单词间是空格的情况,可以加入考虑是空格、英文逗号,句号。

while (str1[j]==32||str1[j]==44||str1[j]==46)    //这里要用while循环  不能用if语句
            j++;
        while(j<len&&str1[j]!=32&&str1[j]!=44&&str1[j]!=46)    //j<len很重要  不然统计字符串最后一个单词时while循环会一直执行下去  直到字符串末尾
            str2[i][k++]=str1[j++];
改一下就可以了。

下面是华为的一道题:

题目描述:

输入一段英文文本,用程序统计出现频率最高和最低的两个单词;

英文文本中仅出现这四类字符:空格( )、英文逗号(,)、英文句号(.)、英文大小写字母(a-z、A-Z)

单词之间的分隔符仅考虑这三种:空格( )、英文逗号(,)、英文句号(.);

仅大小写不同的单词算同一个单词;

如果两个单词出现次数相同,则在文本中首次出现的单词优先返回。

返回的单词统一用小写字母返回

例如:

输入字符串“Hello world, i said hello world to the world”,返回“world”,“i”

输入字符串“Somebody like somebody,i do not like it”,返回“somebody”,“i”

要求实现函数:

void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord);

//统计字符串pInputStr中出现次数最多的和最少的单词
void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord)
{
    int len=strlen(pInputStr);
    int j=0,i=0,k=0;
    char dst[10000][20];
    int  count[10000];
    while(j<len)
    {
        while(pInputStr[j]==32||pInputStr[j]==44||pInputStr[j]==46)
            j++;
        while(j<len&&pInputStr[j]!=32&&pInputStr[j]!=44&&pInputStr[j]!=46)
            dst[i][k++]=pInputStr[j++];
        dst[i][k]='';
        count[i]=1;
        for (int p=0;p<i;p++)
            if (strncmp(dst[i],dst[p],20)==0)
            {
                count[p]++;
                i--;
                break;
            }
            i++;
            k=0;
    }

    int max=count[0];
    int max_flag=0;
    int min=count[0];
    int min_flag=0;
    int f;
    for (f=0;f<i;f++)
    {
        if (max<count[f])
        {
            max=count[f];
            max_flag=f;
        }
        if (min>count[f])
        {
            min=count[f];
            min_flag=f;
        }
    }
    cout<<dst[max_flag]<<"	"<<max<<endl;
    pOutputHotWord=dst[max_flag];
    cout<<dst[min_flag]<<"	"<<min<<endl;
    pOutputColdWord=dst[min_flag];
}
原文地址:https://www.cnblogs.com/audi-car/p/4637689.html