create new tag
view all tags

Shakespeare in Class file activity

Getting ready for the final project - viewing and search the text.

The goal here is to get everyone to step one on the final project. This project involved building a object orient c++ program that allows you to search for words in the complete works of Shakespeare.

What we will do in this hands on activity is to set up a small program that reads in portions of Shakespeare, and displays it on the screen.

Step 1 - files and project setup

  1. Create a C++ project "demoSSReader" on your system in Netbeans.
  2. Download "Shakespeare.txt" file by right clicking here: Shakespeare.txt and saving it to your system.
  3. Copy the file into the top directory of your project (this will likely be "yourhomedir/NetBeansProjects/demoSSReader")

Step 2 - Simple Program to read Shakespeare into a vector of lines.

  • Consider the following code to create a input file stream, and open it to the Shakespeare file. Add this to your project main function. It also includes code we developed for the Poem lab.
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;
const string SHAKEFILE = "shakespeare.txt";

vector<string> getWords(string line);
string RemoveSpecials(string str);

int main() {

    ifstream shakefile;

    if (shakefile.fail()) {
        cout << "Couldn't open: " << SHAKEFILE << endl;
    //write code to search for matches
    return 0;

vector<string> getWords(string line) {
    istringstream iss(line);
    vector<string> words;
    string s;
    do {
        iss >> s;
        if (s.length() > 0 && s != " ") {
            //cout << s << " ";
    } while (iss);
    return words;


string RemoveSpecials(string str) {
    int i = 0, len = str.length();
    while (i < len) {
        char c = str[i];
        if (((c >= '0')&&(c <= '9')) || ((c >= 'A')&&(c <= 'Z')) || ((c >= 'a')&&(c <= 'z')) || c == '\'' || c == ' ') {
            if ((c >= 'A')&&(c <= 'Z')) str[i] += 32; //Assuming dictionary contains small letters only.
        } else {
            str.erase(i, 1);
    return str;

Write code to read each line and push it into a vector of strings. Test it out. How many lines are there?

Step 3 - Make the program count words

Let's add functionality to ask for a word, and display the lines with the matching word. Also display the number of matching words

Search for Castle. Search for king. It might be nice to print out the lines where you find matches.

Step 4 - Search Book titles

Can you count the number of books, and print the titles

Step 5 - Search Book ends

What line is each on?

Step 6 - Additional ideas

Can you search for a phrase?

Edit | Attach | Watch | Print version | History: r8 < r7 < r6 < r5 < r4 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r8 - 2018-04-26 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback