Start learning free

LESSON 376 OF 409 · PROJECTS: COMMAND-LINE TOOLS

CLI Tool: wc v1 — Count Lines in C++

Practice

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

    Wc V1 in C++

    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free