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

How can I find the number of Strings in a File?

Hey there!
I'm trying to write a program in C++ that will read information in from a file and then tell the user how many strings are in the file and the lengths of the smallest and longest strings. I know I can figure out the length of the strings but I'm having trouble figuring out how to count the number of strings in a code. (It's probably really easy eh?)

Any help at all would be so awesome and greatly appreciated!

Thank you so much!!

Shaye12321
Newbie Poster
6 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

How is 'string' defined for this purpose? Technically any text file only has one string, the entire contents of the file. Another common method is reading words where a 'word' is any unbroken sequence of non-whitespace characters.

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 
How is 'string' defined for this purpose? Technically any text file only has one string, the entire contents of the file. Another common method is reading words where a 'word' is any unbroken sequence of non-whitespace characters.


Exactly. A 'string' can be
- a single word
- a single line
- a sentence
- a paragraph
- a page
- entire file

Be explicit...

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

Exactly. A 'string' can be - a single word - a single line - a sentence - a paragraph - a page - entire file

Be explicit...

Sorry! The strings in question are single lines of no more than 30 characters.

Shaye12321
Newbie Poster
6 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Well you can use a counter variable and every time you read a string increment the counter by 1.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

well.. yeah.. but that doesnt work for everything..
Take a char variable and keep reading one one character from a file..
As soon as the character it reads is a space, increment a value of a variable, say i, by 1...
So in effect, what ur duin is counting spaces!!

After that.. the number of words in ur file are (No. of spaces + 1)

Example
Jason went to buy a loaf of bread

No. of spaces = 7
No. of words = 8 = No. of spaces + 1

P.S - That is the case for all sentences.. But yea, it wont work if u left no spaces between words like a smart ass :P !!

adityatandon
Junior Poster
114 posts since Dec 2011
Reputation Points: 33
Solved Threads: 12
 

@ adityatandon if you include a check for a space and a \n then you will get the right amount of words.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

adityatandon, what part of
Sorry! The strings in question are single lines of no more than 30 characters.
did you not understand?

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: