Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <cctype> #include <algorithm> #include <string> #in

ID: 3586499 • Letter: #

Question

#include <iostream>
#include <cctype>
#include <algorithm>
#include <string>
#include <map>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstdio>

using namespace std;

typedef map < string, int >::const_iterator it_type; //Declaring the map type

const int NO_ITEMS = 3;    //Number of items delcaration, const value
const int ITEM_W = 16;       //Item width declaration, const value.


// isA function, determines if character is alphanumeric or not, returns the result of the std function isalnum()

bool isA ( const char& c){
    return isalnum(c);
}

// isNotA function, determines if character is alphanumeric or not, returns the opposite of the result of the std function isalnum()

bool isNotA ( const char& c){
    return !isalnum(c);
}

// convert_to_low function, takes a character and calls the tolower() std function to convert it to lower case. Returns nothing.

void convert_to_low ( char& c){
    c = tolower(c);
    return;
}

// print_words function, takes a map as an argument and prints the contents. Returns nothing.

void print_words(const map < string, int >& map) {
    int counter = 0;       //Initialize counter for rows
    int number = 0;        //initialize number of words for columns
    for(it_type it = map.begin(); it != map.end(); ++it) { //for loop with iterator to iterate through map
        if(counter == NO_ITEMS){   //If the counter gets to the number of items max allowed in a row,
            counter = 0;               //reset counter and
            cout << endl;              //start a new line.
        }

        number += (*it).second;           //increment number with the map value
        cout << setw(ITEM_W) << left << (*it).first << " : " << setw(3) << (*it).second << "    ";   //cout statement with formatting
        counter++; //increment counter value.
    }

    cout << endl << endl; //formatting
    cout << "number of words in input stream : " << number << endl; //text to print at end of output
    cout << "number of words in output stream : " << (int)map.size(); // ^^^

}

// clean_entry function, takes 2 strings as arguments, removes all punctuation characters, and sets the words to lowercase.

void clean_entry( const string& word1, string& word2) {
    int index = 0;      //initialize index value to determine first alnum character
    int indexEnd = 0;   //initialize indexEnd value to determine space after last alnum character

//setting bool values to determine formatting requirements

    bool hasPunc = false;
    bool start = false;
    bool end = false;

    for(int count=0; count<(int)word1.length(); count++){
        if(isNotA(word1[count])){ //If there is no alnum character, hasPunc is set to true
            hasPunc = true;
        }

        if(isA(word1[count]) && start == false){
            start = true; //If there is an alnum character and start is still false, will set start equal to true and index to count.
            index = count;
        }

        if(start == true){             //If start is true and isNotA returns true, end will set to true and indexEnd will equal count - This defines the location of the word without punctuation characters
            if(isNotA(word1[count])){
                end = true;
                indexEnd = count;
            }
        }
        if(end == true){ //If the end value has been found, break out of loop - this is what ends the word with non alnum characters in the middle of the word. (i.e. fish-sticks to just fish)
            break;
        }
    }

    word2 = word1.substr(index, indexEnd - index);   //Sets word2 equal to word1's substring with the boundaries being the indexes from above.

    for_each(word2.begin(), word2.end(), convert_to_low); //calls the convert_to_low function to change characters to lowercase.

    if(hasPunc==false){ //If the hasPunc is false, there is no need to use a substr boundary, and word2 can just equal the word1 in its entirety.
        word2 = word1;
        for_each(word2.begin(), word2.end(), convert_to_low); //calls the convert_to_low function to change characters to lowercase.
    }


}

// get_words function, gets the words from the input and calls the clean_entry function to clean the words, then deposits them into the map. Takes a map and the input file as arguments, returns nothing.

void get_words(map<string, int>& map, ifstream& infile) {
    string toBeCleaned, word;

    while (infile >> toBeCleaned){                       //While new words are retrieved
        clean_entry(toBeCleaned, word);                    // call clean_entry with toBeCleaned value and word value
        if (word.length() > 0){                           // if the returned value is greater than 0,
            map[word]++;                               //     deposit into map.
        }
    }

    if(infile.eof()){                               //If the eof is reached,
        return;                                       // return back to main().
    }


}

int main(int argc, char** argv) {
//    if ( argc < 2 ) {                                               // if the argument line isn't used correctly, prints error message.
//        cerr << "command line argument: input.txt" << endl;
//        return 1;
//    }

    ifstream infile("C:\Users\Swapnil\CLionProjects\Words\input.txt");        //Reads in the input file from the command line with ios::in

    map<string, int> m;   //Declares map m

    get_words( m, infile ); //calls get_words function to get the words into the map

    infile.close();   // closes input file

    print_words( m ); //calls print_words to print contents of the map

    return 0;

}

Want to have header file with prototypes included in seperate .h file, Can you help please

Explanation / Answer

Here is the header file Separator.h for you:

#ifndef SEPARATOR_H
#define SEPARATOR_H
#include <iostream>
#include <fstream>
using namespace std;
bool isA ( const char& c);
bool isNotA ( const char& c);
void convert_to_low ( char& c);
void print_words(const map < string, int >& map);
void clean_entry( const string& word1, string& word2);
void get_words(map<string, int>& map, ifstream& infile);
#endif