Count the number of English words in a file

Source: Internet
Author: User

/* Count the number of English words in a file, and output the total number of English words by Word = count in the format order */#include <fstream> #include <iostream> #include < string> #include <vector> #include <algorithm>using namespace Std;int main () {string line;// Open the input file Ifstream input ("Transform.txt"),//Open output file Ofstream outputs ("Result.txt"),//use two vectors to achieve the function of map, the difference is to count the words sequentially, Instead of the key value vector<string> wordvec;vector<int> Countvec (200);//the word count int wordcount=0;//read one line at a time (Getline ( Input,line) {size_t pos=0;//first divides the string by a space while (Pos!=line.size ()) {if (line[pos]!= ') {string word;while (Pos!=line.size ( ) && line[pos]!= ') {word+=line[pos];++pos;} Remove the substring of the leading punctuation int prepos=0;while (prepos!=word.size () && ispunct (Word[prepos])) {++prepos;} Get rid of substring suffix punctuation//Note unsigned number and signed number int pofixpos=word.size () -1;while ((pofixpos>=0) && ispunct (Word[pofixpos])) {--pofixpos;} Skip the statistical action of invalid words if (prepos==word.size () | | pofixpos<0) continue;else++wordcount;//get processed words, count the number of words string pureword= Word.substr (prepos,pofixpos-prepos+1); vector<string>::iTerator Iter=find (Wordvec.begin (), wordvec.end (), Word), if (Iter==wordvec.end ()) {countvec[iter-wordvec.begin ()]++; Wordvec.push_back (Pureword);} Elsecountvec[iter-wordvec.begin ()]++;if (Pos==line.size ()) break; ++pos;}} Close the input file Input.close ();//The detailed results of the word statistics vector<string>::iterator traiter=wordvec.begin (); vector<int>:: Iterator Coniter=countvec.begin ();output<< "total Words:" <<wordcount<<endl;while (traiter!=wordvec.end ()) {output<<*traiter<< "=" <<*coniter<<endl;++traiter;++coniter;} Close output file Output.close (); return 0;}

Input file Transform.txt content:

My father was a self-taught mandolin player. He is one of the best string instrument players He could ' t read music, but if he heard a tune a few times, he could play it. When he is younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band the featured Patsy Cline as their lead singer.

Output File Result.txt content:

Total words: 91My = 1father = 1was = 4a = 8self-taught = 1mandolin = 1player = 1He = 3one = 1of = 2the = 2best = 1string = 1instr ument = 1players = 1in = 2our = 1town = 1could ' t = 1read = 1music = 2but = 1if = 1he = 5heard = 1tune = 1few = 2times = 1c Ould = 1play = 3it = 1When = 1younger = 1member = 1small = 1country = 1band = 2They = 1would = 2at = 1local = 2dances = 1a nd = 2on = 1occasions = 1for = 1radio = 1station = 1often = 1told = 1us = 1how = 1had = 1auditioned = 1earned = 1position = 1that = 1featured = 1Patsy = 1Cline = 1as = 1their = 1lead = 1singer = 1

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Count the number of English words in a file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.