Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

number of occurences of a specific text in a file using c++

Posted on 2011-03-08
2
Medium Priority
?
284 Views
Last Modified: 2012-05-11
Hi,

I have a huge text file. I would like to know, for example, how many times "Mumbai" is present in the file. Can anyone suggest a quick, efficient way of doing this using c++, STL etc? Thanks in advance.

0
Comment
Question by:James Bond
2 Comments
 
LVL 5

Accepted Solution

by:
TomasP earned 1000 total points
ID: 35369632
From the site
http://www.cplusplus.com/reference/string/string/find/// string::find
First load the file into a string
Then put the the find logic below in a loop and every time you find "Mumbai" increment a counter
Make sure you increment found by 1 so the search doesn't find the same "Mumbai" repeatedly but rather searches the whole string

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("There are two needles in this haystack with needles.");
  string str2 ("needle");
  size_t found;

  // different member versions of find in the same order as above:
  found=str.find(str2);
  if (found!=string::npos)
    cout << "first 'needle' found at: " << int(found) << endl;

  found=str.find("needles are small",found+1,6);
 if (found!=string::npos)
    cout << "second 'needle' found at: " << int(found) << endl;

  found=str.find("haystack");
  if (found!=string::npos)
    cout << "'haystack' also found at: " << int(found) << endl;

  found=str.find('.');
  if (found!=string::npos)
    cout << "Period found at: " << int(found) << endl;

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");
  cout << str << endl;

  return 0;
0
 

Author Closing Comment

by:James Bond
ID: 35372055
Thanks. This was a great help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question