Lab (Search)

Programming Workshop 2 (CSCI 1061U)

Faisal Qureshi

Faculty of Science, UOIT


You are asked to develop a C++ program that can count the occurences of a string in a text file. Consider, for example, the following text file

2015 2015

If we save this file as file.txt and use our program to process it as follows, we will get the following output:

$ ./sr file.txt 2014
Reading file.txt

"2014" was found 0 times.


$ ./sr file.txt 2015
Reading file.txt

"2015" was found 2 times.

The above examples suggest that your program can be used to count the occurence of a substring within a text file. Furthermore that your program takes two arguments: 1) the name of the text file and 2) the substring to match.

Programming considerations

You are asked to use std::ifstream and std::string to complete this lab. My sr.cpp file looks like this

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

//#define DEBUG

int main(int argc, char** argv)
  // code goes here 
  return 0;

Don't forget to try your program on some big text files. Check out the file-big.txt provided with this lab.

What to Submit?

Submit your source and make files via Blackboard.