Home>

There is text. It is necessary to calculate how many words were used. Example:
The word "The" was used 2 times
The word "PROMISE" was used 3 times
The word "to" was used 4 times
etc.
At the moment, my program considers only the number of words in the text.

#include <
iostream >
#Include <
String >
#Include <
FSTREAM >
#Include <
stdio.h >
#Include <
Windows.h >
#Define Strlen 255.
Using Namespace STD;
INT MAIN (Char Arrword) {
    SetConsoleCP (1251);
    SetConsoleoutPutcp (1251);
    String Path= "file.txt";
    IFStream MyFile;
    MyFile.open (PATH);
    if (! myfile.is_open ())
    {
        COUT <
≪
 "File is not open" <
≪
 Endl;
        Return 0;
    }
    ELSE.
    {
        COUT <
≪
 "The file is successfully opened" <
≪
 Endl;
    }
    Char str [Strlen];
    char * p;
    INT COUNT_ALL, COUNT_WORD;
    Count_all= Count_Word= 0;
    While (! myfile.eof ())
    {
        MyFile >
≫
 STR;
        FOR (P= STRTOK (STR, ""); P!= NULL; P= STRTOK (NULL, "")) {
            ++ Count_all;
        }
    }
    Printf ("Total Words:% d \ n", Count_all);
    SYSTEM ("PAUSE");
    Return 0;
}
c++
  • Answer # 1

    Conduct your implementation of this task. If desired, it is enough to simply modify in accordance with its needs. To solve, I used the MAP container. Data is read from a text file (both in your program). As a result, the program displays the word and then the number of times it has found in the text, as well as the total number of words.

    Below I present the implementation on c++, which is equipped with my comments:

    #include <
    iostream >
    #Include <
    FSTREAM >
    #Include <
    MAP >
    #Include <
    String >
    Using Namespace STD;
    //Check if the character is the sign of punctuation
    BOOL CHECK (CHAR S) {
        Return (
            s== '.' Or.
            s== ',' or
            s== ':' or
            s== ';' Or.
            s== '?' Or.
            s== '!' Or.
            s== '"' or
            s== ')' or
            s== '('
            );
    }
    //Conclusion of the contents of the dictionary on the screen
    Void Show (MAP <
    String, int >
     M, INT COUNT_ALL) {
        MAP <
    String, int >
     :: ITERATOR IT= M.BEGIN ();
        FOR (int i= 0; it!= m.end (); it ++, i ++) {//withdraw them
            COUT <
    ≪
     I <
    ≪
     . Word: "<
    ≪
     IT->
    FIRST <
    ≪
     "-USED" <
    ≪
     IT->
    Second <
    ≪
     Endl;
        }
        //COUT <
    ≪
     "========================"<
    ≪
     Endl;
        COUT <
    ≪
     "Total Word In This Text:" <
    ≪
     Count_all <
    ≪
     Endl;
    }
    INT MAIN () {
    ifstream file ("input.txt"); //considered the file, you can specify your way
    //Used variables
    String Temp_Word;
    MAP <
    String, int >
     word_stat; //Word Storage Dictionary
    INT COUNT_ALL= 0; //Total number of words in the text
    STRING TEXT;
    GetLine (File, Text); //Read the text from the file in the string
    //Delete all the signs of punctuation. It is assumed that in words they are not.
    for (int i= 0; i <
     text.length (); I ++) {
        if (Check (Text [i])) Text [i]= '';
    }
    int i= 0;
    While (I <
     text.length ()) {
        String Temp_Word= "";
        //We are looking for a word, because the words are separated now spaces
        IF (Text [i]!= '') {
            While (Text [i]!= '' and i <
     text.length ()) {
                TEMP_WORD= TEMP_WORD + TEXT [I];
                I ++;
            }
            WORD_STAT [TEMP_WORD] ++;
            COUNT_ALL ++;
        }
        I ++;
    }
    Show (Word_stat, Count_all);
    }
    

    Of course, I described a fairly simple approach, which could be improved, for example, bring words to Lemma (that is, the initial form) to the words Ball & Balls were considered as one word, but it is already another task=)