By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

Count occurrences of a string in a file

P: 1
Hello everybody!
I'm a so-called newbie at programming in C/C++ and I'm currently working on a poject that searches for a particular string in a file. The user enters the string he/she is searching for, and the program is supposed to return the number of occurrences of the string in the file along with the line numbers at which the string occurs, or an error message saying string not found.

I've managed to write the code for searching for the string (thanks to help I got off the net), but I still need some more help in printing the number of occurrences of the string in the file (if the string is found) along with the line numbers at which the string occurs.
I would really appreciate it if there's anyone out there who's more experienced than I am, and willing to help me out. I would greatly welcome any suggestions or ideas you all might have to throw at me concerning my need for help.

You can e-mail on either one of the following addresses:
<removed per posting guidelines>


Have a smashin' day!...and thank you in advance! ;-)
Beeteh
Jun 7 '07 #1
Share this Question
Share on Google+
3 Replies


Savage
Expert 100+
P: 1,764
No emailing here,everything is done here on the forum.Please read psting guidelines.

:)

Savage
Jun 7 '07 #2

100+
P: 208
Have a smashin' day!...and thank you in advance! ;-)
Beeteh[/quote]

At least (S)he's happy :P
Jun 7 '07 #3

100+
P: 256
I've managed to write the code for searching for the string (thanks to help I got off the net), but I still need some more help in printing the number of occurrences of the string in the file (if the string is found) along with the line numbers at which the string occurs.
Finding the strings is the hard part, to get the count and line numbers you just need to add in couple of counter variables (int type is probably fine).

Increment one counter each time you read in a line, then when ever you find a matching string you just print the value of this variable to show the line it was found on.

Increment the other counter each time you find a matching string. This will tell you the total number of occurrences.

I really can't give any more advice than that without actually doing your assignment for you.
Jun 7 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.