Start learning free

LESSON 11 OF 69 · PROJECTS: COMMAND-LINE TOOLS

wc Character Count in C#: Summing String Lengths

Practice Read the full explanation

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

Convention: count only the characters in each line — do not count the implicit newlines that separate them. Spaces and tabs inside a line count as one character each.

Note: this is a simplified version. The real wc -m also counts the newlines, and the real wc -c counts bytes, not characters. Here the answer is simply the sum of the line lengths. The input is plain ASCII text (other alphabets are counted differently by different languages).

Function signature:

public int CountChars(List<string> lines)

Example:

Input: lines = ["hello", "world"]

Output: 10

"hello" has 5 chars and "world" has 5 chars, for a total of 10.

An empty line adds 0, and an empty list of lines gives 0.


Loop through the lines and accumulate line.Length into an int running total.

The full solution:
class Solution {
    public int CountChars(List<string> lines) {
        int total = 0;
        foreach (string line in lines) {
            total += line.Length;
        }
        return total;
    }
}

STEP 1 OF 4
    Ctrl + Enter to check

    C# · PROJECTS: COMMAND-LINE TOOLS

    Wc V3 in C#, explained

    Problem overview

    CountChars takes a List<string> of lines and returns the total number of characters in them. This is a simplified take on the character count of wc: only the characters inside each line count, not the newlines that would separate the lines in a file. Spaces and tabs are characters like any other, one each.

    For ["hello", "world"] the answer is 5 + 5 = 10. An empty line adds 0, and an empty list gives 0.

    Approach

    The answer is the sum of the line lengths. A string in C# knows its own length, so there is nothing to scan or split: visit each line, take its Length, and add it to a running total.

    The problem is small on purpose. It sets up the loop-and-accumulate pattern that the next lesson, which combines all the wc counts, reuses.

    Step by step

    1. Create total. An int that starts at 0, before any line has been looked at.
    2. Write a foreach loop over lines, with each entry named line. A list can be walked this way without an index.
    3. Add line.Length to total inside the loop. Every line contributes its own length, spaces and tabs included.
    4. Return total after the loop.

    The solution in C#

    class Solution {
        public int CountChars(List<string> lines) {
            int total = 0;
            foreach (string line in lines) {
                total += line.Length;
            }
            return total;
        }
    }
    

    The details worth knowing:

    • Length without parentheses. On a string and on an array, Length is a property. On a List the size is Count. Writing line.Length() is a compile error.
    • Length counts UTF-16 code units. For plain ASCII text, which is all this lesson uses, that is the same as the number of characters. Some characters outside ASCII can take two units, which is why the statement restricts the input.
    • Tabs are one character. "a\tb" has length 3: the \t in the source is a single tab character.
    • Strings are immutable. Reading Length never changes the line.

    The same sum with LINQ

    C# can also express the loop as one expression. With using System.Linq;, lines.Sum(l => l.Length) returns the same total, and it returns 0 for an empty list. The explicit loop is what the lesson builds, and it is the clearer way to see how a running total works, which is why the steps use it. Both versions read each line once.

    The variable total is a plain int. It can hold values up to about 2.1 billion, far more than a list of text lines will add up to in the lesson's tests, and if it did overflow, C# would wrap around silently rather than report an error.

    Complexity

    Time: O(n), where n is the number of lines. Reading a string's length takes constant time, because the string stores it, so the cost does not depend on how long the lines are. Space: O(1), a single integer.

    Edge cases and common mistakes

    • Adding the newlines. The convention here is not to count them. The example would then report 11 instead of 10 (one newline between two lines).
    • Trimming the line first. Spaces count in this lesson, so call Length on the line as it is.
    • Counting lines instead of characters. lines.Count is the number of lines, 2 for the example.
    • An empty list or empty lines. Both add 0, with no special case.

    Testing

    • ["hello", "world"] gives 10.
    • ["a b", "", "\t"] gives 4.
    • [] 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