一串字符串中出现次数最多的单词

来源:互联网 发布:中联汽车电子公司 知乎 编辑:程序博客网 时间:2024/05/16 15:44
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
void fun(char a[])
{
    char str[100][100];
    char temp[200];
    int value[200];
    int k1=0,k2=0,k3=0;
    int i=0,j=0;
    while (a[i]!='\0')
    {
        if (i==0)
        {
            while (a[i]!=' ')
                temp[k3++]=a[i++];


            temp[k3]='\0';
            k3=0;
            strcpy(str[k1],temp);
            value[k1++]=1;
        }
        else
        {
            if (a[i]==' ')
                i++;
            while (a[i]!='\0'&&a[i]!=' ')
                temp[k3++]=a[i++];


            temp[k3]='\0';
            k3=0;
            j=0;
            while (j<k1)
            {
                if (strcmp(str[j],temp)==0)
                {
                    value[j]+=1;
                    break;
                }
                j++;
            }
            if (j==k1)
            {
                strcpy(str[k1],temp);
                value[k1]=1;
                k1++;
            }


        }
    }
    cout<<value[1]<<endl;
    //cout<<str[0]<<endl;
}
int main()
{
    char a[]="aa aa aa aa aa aa aa aa aa aa amedede am am am jnnk adfp crpcwe crwc qa qa qa qa qa qa qa qa qa ";
    fun(a);
    return 0;
}
0 0
原创粉丝点击