954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++/ How to count words

In file i have a text. Words form line to line dont raising. Word in line separate least one space.Spaces can be at line begining and ending, can be empty lines. Count how many words can be in real number.
i need a function whitch can count words whitch can be real number.

I started like that this was first task, and now i need to count words, please help me ;/

const char Cduom[] = "Duomenys.txt";
const char Crez[] = "Rezultatai.txt";
const char Canalize[] = "Analize.txt";
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

void ApdorotiTeksta(const char dfv[], const char rfv[]);
void AnalizuotiEilute(string eil);


int main()
{
  ApdorotiTeksta(Cduom, Crez);
}


void ApdorotiTeksta(const char dfv[], const char rfv[])
{
 
  ifstream fd(dfv);
  ofstream fr(rfv);

  string E;

  while(!fd.eof()) {
    getline(fd, E);
   
  
   
    fr << E << endl;
  }

  fd.close();
  fr.close();
}

void AnalizuotiEilute(string eil )
{
	string Zodis;
  string Skirt = " ,!?:;()\t"; 
 
  int zpr = 0, zpb = 0;

  
  while ((zpr = eil.find_first_not_of(Skirt, zpb)) != string::npos) {
    zpb = eil.find_first_of(Skirt, zpr);
    Zodis = eil.substr(zpr, zpb - zpr);
         
  
}
}
EvaL2ne
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Dont read it line by line. Instead, read it word by word. The check if each word is a valid real number. Example:

int main(){
 ifstream fileIn("input.txt");
 string word;
 unsigned int count = 0;
 while( fileIn >> word )
 {
    if( isRealNumber( word ) ){  //you need to implement isRealNumber
        ++count;
    }
 }
}


Note in the above I assume each words are separated by spaces

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

I dont know how to count a words, im beginer :/

EvaL2ne
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Read a word fin >> word;
Increment counter numwords++;

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: