Counting Words
Part of the Logic & Flow section of Coddy's Dart journey — lesson 54 of 65.
Challenge
EasyContinuing 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.
- Use the
textToAnalyzevariable from your previous setup - Use the
totalCharactersvariable from your character counting step - Use the
wordListvariable from your word splitting step - Count the total number of words by using the
.lengthproperty on the word list - Store the word count in a variable called
totalWords - Display the word counting analysis results in the exact format shown below
- 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
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart8Project: Simple Text Analyzer
Project Overview and SetupCounting Characters3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List