Information about the words in the statistics file

Source: Internet
Author: User
Tags vmin
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cctype>
#include <algorithm>

using namespace Std;

int main (void)
{

Ifstream finput ("String.txt", ifstream::in);

Vector<string> vmax,vmin;
string Word;
size_t wcnt = 0,ccnt = 0; Wcnt count characters (excluding spaces) ccnt count the number of words (not including numbers)
String::size_type max_size = 0, min_size = 20;

while (finput >> Word)//from the string input stream type read word
{
ccnt + + word.size ();
if (word.find_first_of ("0123456789") = = String::npos)
{//excluding numbers
++wcnt;
if (Ispunct (Word.back ()))
{//delete punctuation in Word
Word.pop_back ();
}
if (Word.size () <= min_size)
{//Processing length min word
if (word.size () = = min_size)
{
if (Vmin.begin (), vmin.end (), word) = = Vmin.end ())//
{//filter duplicate words
Vmin.push_back (word);
}
}
Else
{
Min_size = Word.size ();
Vmin.clear ();
Vmin.push_back (word);
}
}
if (Word.size () >= max_size)
{//Handle the longest word in length
if (word.size () = = max_size)
{
if (Vmax.begin (), vmax.end (), word) = = Vmax.end ())
{//filter duplicate words
Vmax.push_back (word);
}
}
Else
{
Max_size = Word.size ();
Vmax.clear ();
Vmax.push_back (word);
}
}
}
}
cout << "There are" << ccnt << "characters in the sentence (except spaces)" << Endl;
cout << "There are" << wcnt << "Words in the sentence" << Endl;
cout << "Maximun word (s):";
for (Vector<string>::iterator iter = Vmax.begin (); Iter!= vmax.end (); ++iter)
{
cout << *iter << "";
}
cout << Endl;
cout << "Minimun Word (s):";
for (Vector<string>::iterator iter = Vmin.begin (); Iter!= vmin.end (); ++iter)
{
cout << *iter << "";
}
cout << Endl;

return 0;
}
Related Article

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: info-contact@alibabacloud.com 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.