Click here to Skip to main content
15,878,230 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
while(fgets(temp1, 512, my_file) != NULL) {
if((strstr(temp1, str)) != 0) {
l++;
printf(" match found on line: %d\n", sr_num);
printf("\n%s  \n", temp1);


result++;
                }



/*this is returning same result  for strings like mc Larenc,mc Cody ,mc Larenc*/


What I have tried:

i need to check frequency of strings and its adding the frequency of strings like mc Larenc,mc Cody ,mc Larenc,mc Donell just checking first two characters not the whole string
Posted
Updated 6-Oct-18 22:21pm
v2
Comments
Richard MacCutchan 6-Oct-18 10:30am    
What are the values in temp1 and str?
Richard MacCutchan 6-Oct-18 11:46am    
But what are the values in them? How can we help you unless you give us full details?
Richard MacCutchan 6-Oct-18 11:53am    
Well you still have not explained what you are comparing against. What (for the fourth time) is the actual value in the str variable?
Richard MacCutchan 6-Oct-18 12:06pm    
Well I have just tried that and it correctly returns the match count as 1. Whatever you are doing in your code is different to what you think. Also, it would probably make more sense to use strcmp rather than strstr.
Richard MacCutchan 6-Oct-18 12:15pm    
Yet another incomplete question I am afraid. Saying "it wasn't working" tells us absolutely nothing.

Please edit your question, delete what is there and show the actual code you are using. Also show the actual values of the variables that you are using in the comparisons.

Incidentally, even if your code was only comparing the first two letters, "mc", in its tests, it still should not get a count of 5 as there are only 4 occurrences of that string in your data.

OK, I found your problem. You are using scanf to get the string to search for, with the following code:
C++
printf("enter the name you want to search \n");
scanf("%s",j);

result = Search_in_File("name.csv", j);

BUT, scanf treats white space as a token separator, so if you enter "mc Larenc", it will only extract the "mc" part as the value to be stored in j. So you are counting each string that contains the string "mc". Using strcmp might have helped you find the mistake sooner.
 
Share this answer
 
Comments
Member 14009930 6-Oct-18 12:49pm    
but using strcmp is returning all the values that are in the list
Richard MacCutchan 6-Oct-18 14:04pm    
I'm sorry but I have tried really hard to get you to understand that vague statements like this are not helping us, or more importantly, you. Unless you give us proper details, and show the code and the data that you are using, we have no way of figuring out what you are doing wrong.
Member 14009930 6-Oct-18 14:07pm    
that's the whole code i'm running man and this is my file



o Lenan
McCormuck Dun
Dun
o Linaghe
McMorris
Bane
McMortagh
Keaton
Keaton
Keaton
Keaton
o ffeagh
Hefernon
Cosby
Mase
Mase
Stafford
Sutton
Sutton
Alcock
Protector
Stafford
Stafford
Clandalke
Cheevers
mc Cody
Debbon
Vynes
Vicars
ffenen
Browne
McKillpatricke
McMurrogh
Vinecomb
Wicken
starrford
Synnott
Synnot
Synnot
wicken
Waller
Wicken
Cheeuers
mc Murtagh
mc Cody
mc Donell
waller
Wagstaffe
Wagstaffe
Dunne
mc Larenc



just input any random value out of this for str and run the program you will get frequency values but it shows wrong value when i input something like "mc Larenc" which has space in between
Richard MacCutchan 7-Oct-18 2:52am    
Yes, and I already told you why.
Do not use scanf (as already suggested), instead use gets_s, _getws_s | Microsoft Docs[^] which will read a complete line.

Do not use strstr as it does not do a full compare. Use strcmp, wcscmp, _mbscmp | Microsoft Docs[^] (as already suggested).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900