Word Counter

Analyze an array of strings using len, iteration, mutable accumulators, and comparisons.

1

Core concepts

array[string], len, loops, string comparison

2

How the learner should approach this project

Do not paste the complete program immediately. Create the file, type the first step, run vpp check, and then run the program when a complete entry point exists. Read the explanation before looking at the snippet. After each step, predict what the new code should do. This turns the page into a lesson rather than a code dump.

3

Step 1: Count total characters

len works on a string and returns its character count for the purpose of this lesson. The accumulator starts at zero and adds each word length.

After typing the snippet, identify the new names introduced by the step. Ask what each name represents, what type the compiler should infer or check, and what value should exist after the code runs.

main.vpp
fn total_chars(words: array[string]) -> int {
    let mut total = 0
    for w in words {
        total = total + len(w)
    }
    return total
}
4

Step 2: Find the longest word

The first word becomes the initial best candidate. Each later word is compared by length. When a longer word appears, best is updated.

Before moving forward, explain how this step connects to the previous one. In particular, identify which values cross a function boundary, which values change, and which values are guaranteed by the type system.

main.vpp
fn longest_word(words: array[string]) -> string {
    let mut best = words[0]
    for w in words {
        if len(w) > len(best) {
            best = w
        }
    }
    return best
}
5

Step 3: Analyze the input

The main function creates the word list and asks for the number of elements, total character count, and longest word.

Keeping each calculation in a separate function makes the program easier to extend.

Before moving forward, explain how this step connects to the previous one. In particular, identify which values cross a function boundary, which values change, and which values are guaranteed by the type system.

main.vpp
fn main() -> int {
    let words = ["v++", "is", "a", "compiled", "language"]
    print(len(words))
    print(total_chars(words))
    print(longest_word(words))
    return 0
}
6

Complete source

main.vpp
fn total_chars(words: array[string]) -> int {
    let mut total = 0
    for w in words {
        total = total + len(w)
    }
    return total
}
fn longest_word(words: array[string]) -> string {
    let mut best = words[0]
    for w in words {
        if len(w) > len(best) {
            best = w
        }
    }
    return best
}
fn main() -> int {
    let words = ["v++", "is", "a", "compiled", "language"]
    print(len(words))
    print(total_chars(words))
    print(longest_word(words))
    return 0
}
7

Expected behavior

The complete program should produce the following output when run with the command shown below. Exact formatting should follow the current V++ runtime.

5
16
compiled
8

Run the project

terminal
vpp run projects/16-word-counter/main.vpp
9

What the learner should understand after this project

String analysis is a practical example of combining arrays, loops, len, comparisons, and mutable accumulators.

10

Common mistakes to teach

  1. Reading words[0] from an empty array
  2. Forgetting to update the accumulator
  3. Comparing strings when the algorithm needs their lengths
11

Practice extension

Add a function that returns the shortest word. Test it with an additional word.

A strong learner should be able to explain the program without looking at the code, rebuild the core idea from memory, and make the practice change without copying a solution.

12

Full program

main.vpp
fn total_chars(words: array[string]) -> int {
    let mut total = 0
    for w in words {
        total = total + len(w)
    }
    return total
}
fn longest_word(words: array[string]) -> string {
    let mut best = words[0]
    for w in words {
        if len(w) > len(best) {
            best = w
        }
    }
    return best
}
fn main() -> int {
    let words = ["v++", "is", "a", "compiled", "language"]
    print(len(words))
    print(total_chars(words))
    print(longest_word(words))
    return 0
}
vpp
$ ready. Click Test program.