Untitled

mail@pastecode.io avatar
unknown
c_cpp
8 months ago
2.0 kB
3
Indexable
Never
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

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, vector <string> vec ){
    for(auto v: vec){
        if(s == v)return true;
    }
    return false;
}

int main()
{
    string wordsfile;
    cout << "enter file with list of correct 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> dict;
    while(words >> s ) dict.push_back(s); // creating dict 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 (auto inp: inps){ // for each word of input file
        bool found = ispresent(inp,dict);

        if(!found){ // if word from input isn't in dict, 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