Word Counter
Analyze an array of strings using len, iteration, mutable accumulators, and comparisons.
Core concepts
array[string], len, loops, string comparison
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.
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.
fn total_chars(words: array[string]) -> int {
let mut total = 0
for w in words {
total = total + len(w)
}
return total
}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.
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
}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.
fn main() -> int {
let words = ["v++", "is", "a", "compiled", "language"]
print(len(words))
print(total_chars(words))
print(longest_word(words))
return 0
}Complete source
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
}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
Run the project
vpp run projects/16-word-counter/main.vppWhat the learner should understand after this project
String analysis is a practical example of combining arrays, loops, len, comparisons, and mutable accumulators.
Common mistakes to teach
- Reading words[0] from an empty array
- Forgetting to update the accumulator
- Comparing strings when the algorithm needs their lengths
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.
Full program
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
}