1, simple statistics of the number of words, words and words only consider the case of space
//word_statistic.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>#include<string>using namespacestd;#defineM 10000#defineN 20int_tmain (intARGC, _tchar*argv[]) { Charstr1[m]={0}; CharStr2[m][n]; intCount[m]; Gets (STR1); cout<<"The string you entered is:"<<str1<<Endl; intLen=strlen (STR1);//only the Strlen function can be used here, and the sizeof function will output the length of the entire array instead of the actual number of characters in the array . intj=0, i=0, k=0; while(j<Len) { while(str1[j]== +)//The if statement is not used here to use the while loopJ + +; while(j<len&&str1[j]!= +)//J<len is important. Otherwise the last word of the statistic string is executed until the end of the stringstr2[i][k++]=str1[j++]; STR2[I][K]=' /';//count[i]=1;//Word Count for(intp=0;p <i;p++)//Note that the scope of the For loop has persisted to k=0; the Code if(strncmp (str2[p],str2[i],n) = =0)//returns 0 only if the first n characters are equal{Count[p]++;//Note that this is count[p]++, not count[i]++ .i--;//The current word is already duplicated so the current word is not counted into the str2 array Break; } I++; K=0; } //Output Results for(intq=0; q<i;q++) {cout<<str2[q]<<"\ t"<<count[q]<<Endl; } return 0;}
The above code only consider the case of space between words, can be added to consider is a space, comma, period.
while (str1[j]==| | str1[j]==| | str1[j]==) // This is a while loop where you cannot use the IF statement j + +; while (j<len&&str1[j]!=&&str1[j]!=-&&str1[j]!=) // J<len is important. otherwise the last word of the statistic string is executed until the end of the string str2[i][k++]=str1[j++];
You can change it for a second.
Here's a question for Huawei:
Title Description:
Enter a section of English text, using the program to statistics the highest and lowest frequency of two words;
Only these four types of characters appear in the English text: space (), comma (,), English period (.), English uppercase and lowercase letters (A-Z, A-Z)
The delimiters between words only consider these three kinds: space (), comma (,), English period (.);
The same word is counted only in the case of different words;
If two words occur the same number of occurrences, the first occurrence of the word in the text returns first.
Returned words are returned uniformly in lowercase letters
For example:
Input string "Hello world, I said hello World", Return "world", "I"
Input string "Somebody like somebody,i don't like it", return "Somebody", "I"
Required implementation functions:
void Wordstat (const char * pinputstr, char * poutputhotword, char * poutputcoldword);
//count the most occurrences and fewest words in a string pinputstrvoidWordstat (Const Char* Pinputstr,Char* Poutputhotword,Char*Poutputcoldword) { intlen=strlen (PINPUTSTR); intj=0, i=0, k=0; Chardst[10000][ -]; intcount[10000]; while(j<Len) { while(pinputstr[j]== +|| pinputstr[j]== -|| pinputstr[j]== $) J++; while(j<len&&pinputstr[j]!= +&&pinputstr[j]!= -&&pinputstr[j]!= $) Dst[i][k++]=pinputstr[j++]; DST[I][K]=' /'; Count[i]=1; for(intp=0;p <i;p++) if(STRNCMP (Dst[i],dst[p), -)==0) {Count[p]++; I--; Break; } I++; K=0; } intmax=count[0]; intmax_flag=0; intmin=count[0]; intmin_flag=0; intF; 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]<<"\ t"<<max<<Endl; Poutputhotword=Dst[max_flag]; cout<<dst[min_flag]<<"\ t"<<min<<Endl; Poutputcoldword=Dst[min_flag];}
Count the number of words in a string