Home > Back-end >  Can have a big branch explain this word statistics code? Look not to understand blare in the for loo
Can have a big branch explain this word statistics code? Look not to understand blare in the for loo

Time:01-10

# include
# include
Int main () {
Char m [100], word [9];
Int I, j, ans, counts=0, flag.
Gets (m); Gets (word);
for(i=0; iFor (j=I; jIf (m [j]==' ')//string met Spaces end
{flag=j; break; }
If (flag}
Ans=1;
For (j=I; jIf (m [j]. Word [j] I)={
Ans=0; break; }
}
If (strlen (word)!=flag - I) ans=0;
If (ans==1) counts++;
I=flag;
}
Printf (" % s % d ", a word, counts);
return 0;
}
If there are better and more understandable code is too good! Thank you very much!!!!!!

CodePudding user response:

The title
Word counts

Description
Input string string [N] (N 100) or less, each words separated by Spaces, word length less than or equal to 8, the input word word [M] (M 8) or less, find out the same words in a string and statistics the number of occurrences of the word information, and output word occurrences of information, empty one space between the data, the main function of the input string and statistical word, write function count () implementation of statistics and information output,

Time limit
1
Memory limit
10000
Category
1

The input description
Each word input line of characters separated by Spaces, input to statistical word,

The output shows that
Output format output: words and the number of occurrences of information, data between an empty one,

The input sample
ABC dog dog cat dog dog the hahe
Dog

The output sample
Dog 4

Tip
Calculation process is realized by using heavy circulation structure, output data between an empty one,

CodePudding user response:

reference 1 floor????? Response:
title
Word counts

Description
Input string string [N] (N 100) or less, each words separated by Spaces, word length less than or equal to 8, the input word word [M] (M 8) or less, find out the same words in a string and statistics the number of occurrences of the word information, and output word occurrences of information, empty one space between the data, the main function of the input string and statistical word, write function count () implementation of statistics and information output,

Time limit
1
Memory limit
10000
Category
1

The input description
Each word input line of characters separated by Spaces, input to statistical word,

The output shows that
Output format output: words and the number of occurrences of information, data between an empty one,

The input sample
ABC dog dog cat dog dog the hahe
Dog

The output sample
Dog 4

Tip
Calculation process is realized by using heavy circulation structure, output data between an empty one,

This is a topic and test data, please thank you bosses!

CodePudding user response:

The building Lord labeling changes are as follows:
 # include 
# include

Int main (int arg c, char * argv [])
{
Char m [100], word [9];
Int I, j, ans, counts=0, flag.

Gets (m);//string
Gets (word);//to find words

for(i=0; iFor (j=I; jFlag=j;//word position subscript
If (m [j]==' ') break;//space, the end of a word
//jump out for (j=I; j//if (flag//flag=strlen (m);
}
Ans=1;//words contrast setting
For (j=I; jIf (m [j].=word [j - I]) {//will start the word and the word of m [j] [j - I] words, j++ & lt; -> J - I
Ans=0;//if different, the ans s
break;//jump out of the loop
}
}
If (strlen (word)!=flag - I) ans=0; In judging//m [] string such as ABC, and the word []=abcd, step on the judgment is can pass,
If (ans==1) counts++;//the above conditions meet, count + 1
I=flag;//will give I a word at the end of the position, the analysis of the direct access to the next word,
}
Printf (" % s % d ", a word, counts);

return 0;
}


The pointer method and according to the topic with function, for reference as follows:
 # include & lt; stdio.h> 

Int count_SubStr (char * SRC, char * sub);

Int main (int arg c, char * argv [])
{
Char * SRC=https://bbs.csdn.net/topics/new char [100], * sub=new char [10].
Int c;

Gets (SRC);
Gets (sub);

C=count_SubStr (SRC, sub);

Printf (" % s % d \ n ", sub, c);

return 0;
}

Int count_SubStr (char * SRC, char * sub)
{
Pos, char * p, * * t;
Int c=0, flag, charlen=strlen (sub);

P=SRC.
While (* p!='\ 0')
{
Flag=0;
While (* p!=' '& amp; & * p!='\ 0') {
P++;
Flag++;
}
If (charlen==flag) {
T=p;
While (flag -) t -;
Flag=0;
Pos=sub;
While (* t==* pos & amp; & * pos!='\ 0') {t++; Pos++; Flag++; }
If (charlen==flag) c + +;
}
P++;
}
return c;
}
  • Related