Menu
Coddy logo textTech

Counting Words

Part of the Logic & Flow section of Coddy's Dart journey — lesson 54 of 65.

challenge icon

Challenge

Easy

Continuing your text analyzer project, you'll now implement the word counting functionality. This step will count the total number of words in your text, building upon the word splitting work from the previous lesson.

  1. Use the textToAnalyze variable from your previous setup
  2. Use the totalCharacters variable from your character counting step
  3. Use the wordList variable from your word splitting step
  4. Count the total number of words by using the .length property on the word list
  5. Store the word count in a variable called totalWords
  6. Display the word counting analysis results in the exact format shown below
  7. Show the original text, character count, word list, and word count with proper formatting

For example, if your text analyzer was set up with "Hello world!", your program should output:

Text Analysis Tool - Word Count
====================================
Analyzing text: "Hello world!"
Total characters: 12
Words found: [Hello, world!]
====================================
Word Count Results:
Total words: 2
====================================
Word counting completed successfully.

If your text analyzer was set up with "The quick brown fox", your program should output:

Text Analysis Tool - Word Count
====================================
Analyzing text: "The quick brown fox"
Total characters: 19
Words found: [The, quick, brown, fox]
====================================
Word Count Results:
Total words: 4
====================================
Word counting completed successfully.

If your text analyzer was set up with "Programming is fun!", your program should output:

Text Analysis Tool - Word Count
====================================
Analyzing text: "Programming is fun!"
Total characters: 19
Words found: [Programming, is, fun!]
====================================
Word Count Results:
Total words: 3
====================================
Word counting completed successfully.

Your program should use the .length property on the word list to determine how many words were found after splitting the text. This word count will provide essential information about the text's complexity and will be used in conjunction with other analysis metrics in the upcoming lessons.

Try it yourself

import 'dart:io';

void main() {
  String? textToAnalyze = stdin.readLineSync();
  int totalCharacters = textToAnalyze!.length;
  
  List<String> wordList = textToAnalyze.split(' ');
  
  print('Text Analysis Tool - Word Splitting');
  print('====================================');
  print('Analyzing text: "$textToAnalyze"');
  print('Total characters: $totalCharacters');
  print('====================================');
  print('Word Splitting Results:');
  print('Words found: $wordList');
  print('====================================');
  print('Word splitting completed successfully.');
}

All lessons in Logic & Flow