Untitled

mail@pastecode.io avatar
unknown
c_cpp
13 days ago
2.1 kB
6
Indexable
Never
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>

using namespace std;

void tolower(string & s){
    for (int i = 0; i < size(s); i++){
        if (isupper(s[i])) s[i] = tolower(s[i]);
    }
    //precondition: is a string
    //post condition: all letters of string are lowercase.
}

bool ispresent(string s, const vector <string> & vec ){
    for(const auto & v: vec){
        if(s == v)return true;
    }
    return false;
}

int main()
{
    string wordsfile;
    cout << "enter file with valid words" << endl;
    getline(cin, wordsfile);
    cout << "enter file with text to be spellchecked" << endl;
    string inpfile;
    getline(cin, inpfile);
    ifstream words(wordsfile);
    string s;
    vector<string> valid;
    while(words >> s ) valid.push_back(s); // creating valid vector with words from dictionary
    vector <string> wrongs;
    vector <string> inps;
    ifstream inp(inpfile);
    while(inp >> s){
        while (size(s)!=0 && !isalnum(s.back())){ // cutting off trailing punctuation mark until alphanumeric or nothing left
            s = s.substr(0, size(s)-1);} // ' - ' leaves a blank line
        //cout << s << endl;

        while (size(s)!=0 && !isalnum(s.front())){ // cutting off preceeding punctuations - mainly 'word
            s = s.substr(1,size(s)-1);
        }
        if (size(s)!=0){ //adds lower case of each word to inps array
            tolower(s);
            inps.push_back(s);
        }
    }
    int count = 0;
    for (const auto & inp: inps){ // for each word of input file
        bool found = ispresent(inp,valid);

        if(!found){ // if word from input isn't in valid, then check if it's already been detected. if not, print and add to detected list
            bool repeated = ispresent(inp, wrongs);
            if (!repeated){
                count++;
                wrongs.push_back(inp);
                cout << inp << endl;
            }
        }

        if (count >= 20) break;

    }
    return 0;
}

Leave a Comment