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

Dani AI

Generated

As and suggested, the first step is to be explicit about what "string" means. Since clarified you want to treat each file line as a string (<= 30 chars), a robust pattern is: open the file, read it line-by-line with std::getline, increment a counter for each line you want to count, and update min/max lengths. That keeps things simple and avoids word-count tricks like counting spaces (see ), while implementing 's counter idea concretely.

Example (line-based, skips blank lines, trims a Windows CR if present):

#include <iostream>
#include <fstream>
#include <string>
#include <limits>
#include <algorithm>

int main(int argc, char* argv[]) {
    if (argc < 2) { std::cerr << "Usage: countlines <filename>\n"; return 1; }
    std::ifstream in(argv[1]);
    if (!in) { std::cerr << "Cannot open file\n"; return 1; }

    size_t count = 0, maxLen = 0;
    size_t minLen = std::numeric_limits<size_t>::max();
    std::string line;
    while (std::getline(in, line)) {
        if (!line.empty() && line.back() == '\r') line.pop_back(); // trim CR
        if (line.empty()) continue; // remove this check if blank lines should count
        size_t len = line.size();
        ++count;
        minLen = std::min(minLen, len);
        maxLen = std::max(maxLen, len);
    }

    if (count == 0) std::cout << "No lines found\n";
    else std::cout << "Count: " << count << "\nShortest: " << minLen << "\nLongest: " << maxLen << "\n";
    return 0;
}

Notes and pitfalls: decide whether to count empty/whitespace-only lines (use find_first_not_of to detect pure whitespace). If you must enforce the 30-char rule, test if (len <= 30) before ++count. Remember std::string::size() reports bytes; for UTF-8 text bytes != displayed characters, so use a Unicode-aware library if you need "characters" instead of bytes. This approach is O(n) time, uses constant extra memory, and should cover the common edge cases you might run into.

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.