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!!

Recommended Answers

All 7 Replies

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.

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...

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.

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

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 !!

commented: Best to read the thread and not post completely worthless information. -4

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

adityatandon, what part of

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

did you not understand?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.