Start learning free

LESSON 9 OF 69 · PROJECTS: COMMAND-LINE TOOLS

CLI Tool wc v1 in C++: Count Lines with vector size()

Practice Read the full explanation

Build a tiny version of the Unix wc command. Given a list of lines, return the total number of lines as an integer.

Function signature:

int countLines(vector<string> lines)

Example:

Input: lines = ["hello", "world", "foo", "bar", "baz"]

Output: 5

Every entry of the list is one line, even an empty string "". An empty list has 0 lines.


The number of lines is just the size of the vector. Return lines.size().

A one-liner:
class Solution {
public:
    int countLines(vector<string> lines) {
        return lines.size();
    }
};

STEP 1 OF 1
    Ctrl + Enter to check

    C++ · PROJECTS: COMMAND-LINE TOOLS

    Wc V1 in C++, explained

    Problem overview

    The Unix wc command counts things in its input, and its -l flag counts lines. The function countLines takes a vector<string> of lines and returns how many there are, as an int.

    For ["hello", "world", "foo", "bar", "baz"] the answer is 5. Every entry is one line, including an empty string, so ["", ""] has 2 lines. An empty vector has 0 lines.

    Approach: ask the vector

    A vector always knows how many elements it holds, and its size method returns that number in constant time. No loop or counter variable is needed. Counting the lines is the same as counting the strings, and what the strings contain doesn't matter at all: an empty string, a string with spaces or a long text each count as one.

    This is the first version of the wc tool, and the next version builds on it.

    Step by step

    1. Return lines.size(). The method is a single statement, because the vector already holds the count.

    The solution in C++

    class Solution {
    public:
        int countLines(vector<string> lines) {
            return lines.size();
        }
    };
    

    The C++ details worth a closer look:

    • size() returns an unsigned type. It is vector<string>::size_type, an unsigned integer. The function's return type is int, so the value is converted on return. That is safe for any realistic number of lines. Writing return (int)lines.size(); would make the conversion visible, and it behaves the same.
    • Method call syntax. size is a member function, so it needs the parentheses even though it takes no arguments. lines.size without them is a compile error.
    • empty() is a different question. lines.empty() answers "are there no lines?" with a bool. The count is size().
    • The copy. The vector is passed by value, so the call copies the strings before counting them. A const vector<string>& parameter avoids that, but the lesson uses the plain value.

    Complexity

    Time: O(1) for the count itself. A vector stores its size, so size() does not walk over the elements. The by-value parameter makes the whole call O(n) in practice, because the vector is copied when the function is called, and that copy comes from the signature, not from the counting.

    Space: O(1) beyond the copy. The function creates no containers of its own and returns a single integer.

    Edge cases and common mistakes

    • Empty vector. size() is 0, so the answer is 0.
    • Blank lines count. ["", ""] gives 2. A solution that skips empty strings would give 0.
    • One line. ["a b c"] gives 1. The words inside a line don't matter for this count.
    • Writing a loop that counts by hand. It works, but it repeats what size() already gives and adds room for an off-by-one error. A for loop is only needed when each line must be inspected.
    • Counting characters instead. Summing the lengths of the strings counts characters, not lines.

    Testing

    Each result is the function's output:

    • ["hello", "world", "foo", "bar", "baz"] gives 5.
    • ["", ""] gives 2.
    • ["a b c"] gives 1.
    • [] gives 0.
    Keep going with C++1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free