Click here to Skip to main content
14,640,678 members
Rate this:
Please Sign up or sign in to 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.
Richard MacCutchan 6-Oct-18 12:27pm
   
See my solution below.
CPallini 6-Oct-18 11:21am
   
You know, you PC cannot guess that, for instance, mc Cody is just one string. You have to enforce this rule (i.e. you have to write code for that).
Richard MacCutchan 6-Oct-18 11:44am
   
So show us what is contained in the two strings you are trying to compare. We cannot guess what your data looks like.
Patrice T 6-Oct-18 11:47am
   
We can't do anything with this code, it is not complete. Show code that can compile.
Show sample input of str and sample file with actual and expected output.
Patrice T 6-Oct-18 12:03pm
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
jsc42 6-Oct-18 11:52am
   
If you are only concerned about the first two chars, why are you comparing the whole strings? Wouldn't it be easier just to check the first 2 chars if that is all you want, e.g. if (temp1[0] == str[0] && temp1[1] == str[1]) printf("same\n"); else printf("different\n"); ?
Rate this:
Please Sign up or sign in to vote.

Solution 2

OK, I found your problem. You are using scanf to get the string to search for, with the following code:
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.
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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).
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100