473,383 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Number of word in one text

6
Hi,

I'm trying to do a program to count the number of words in a text, and show the average of words per line. How can I count the number of spaces and "enters" in a text?

Thanks
Oct 11 '07 #1
12 2078
Ganon11
3,652 Expert 2GB
Forget about counting the spaces; do you know how to find spaces and enters (i.e. newline characters, '\n')? If you know how to search for these, then counting is simple.
Oct 11 '07 #2
plc
6
Ganon11,

I don't know how to use this. How can I use this?


Forget about counting the spaces; do you know how to find spaces and enters (i.e. newline characters, '\n')? If you know how to search for these, then counting is simple.
Oct 12 '07 #3
Ganon11
3,652 Expert 2GB
Do you know how to get input from the file, one whole line at a time? If so, then do you know how to use the find() method of the string class? If so, then you have all the information you need.
Oct 12 '07 #4
oler1s
671 Expert 512MB
Are we even talking C++ here? Or is it C?

But yes, the first step would be getting input properly. With C++, this is easily accomplished with the getline function for strings, and with C, fgets.
Oct 13 '07 #5
plc
6
I'm talking about C++
Oct 13 '07 #6
plc
6
I know how to use the getline, but how can I use find()?

Do you know how to get input from the file, one whole line at a time? If so, then do you know how to use the find() method of the string class? If so, then you have all the information you need.
Oct 14 '07 #7
oler1s
671 Expert 512MB
http://www.cplusplus.com/reference/s...ring/find.html

You'll want to do some reading.
Oct 14 '07 #8
plc
6
I did that:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void main (void)
  7. {
  8.     ifstream fichier;
  9.  
  10.     char ar[10000];
  11.     int i, word=1;
  12.  
  13.  
  14.     fichier.open("F:\\td3_ex3.txt");
  15.  
  16.     while (!fichier.eof())
  17.     {
  18.         fichier.getline(ar,10000);
  19.     }
  20.  
  21.     for (i = 0; i < strlen(ar);i++)
  22.         {
  23.  
  24.  
  25.          if (ar[i] = '  ')
  26.               {   word=word+1; 
  27.          }
  28.     }
  29.  
  30.      cout << "no. of words = " << word;
  31. }


the text that I supose to read are quite long, and it's only showing the first 49 words. What;s the lenght of a char? And what should I do to find the enters?
Oct 14 '07 #9
Laharl
849 Expert 512MB
A two-parameter getline call gets one line, ended by '\n', the newline character that amounts to an enter, which is dropped from the string. getline also clears the string before it appends, so all you'll get is the last line if you use that. Also, if the third parameter, which defaults to '\n', is found by the input reader, it stops getting input even if the character number has not been reached.
Oct 14 '07 #10
Ganon11
3,652 Expert 2GB
Why are you using character arrays in a C++ program!? Using a std::string will make your life much easier here.
Oct 14 '07 #11
You could use the get() function so as to read each character and whenever a white space character excluding '\n' is read it updates the counter.
Oct 15 '07 #12
plc
6
Actually I need both, because I need the to print the average of words per line, so I need the numbers of words and the number of lines.
Oct 15 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jeffrey Bradshaw | last post by:
Hey everybody, I've got a VB.NET program that is trying to access a Word document and I'm having all kinds of trouble. First on is the above. I'm trying to get the number of lines in the document...
6
by: Eric Layman | last post by:
Hi, I have fields from textareas. With a click of a button, php is able to grab these fields and by using header(), convert the output to Ms Word doc. But the outcome of the word doc...
0
by: deboe | last post by:
I am trying to pull in some data from a database (Access) and merge the data to a Word document (basically mail merge). I got the data to fill in the bookmarks in the document but only the first...
4
by: darith | last post by:
Hi all, I want to know code in vb.net to get number of selected text in MS Word. how can we write? Please help me. I need it urgent. in textbox we just write textbox.selectedtext so we can get...
2
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.