Splitting Text into Words
Part of the Logic & Flow section of Coddy's Dart journey — lesson 53 of 65.
Challenge
EasyContinuing your text analyzer project, you'll now implement the word splitting functionality. This step will break down your text into individual words, creating a foundation for word-level analysis in the upcoming lessons.
- Use the
textToAnalyzevariable from your previous setup - Use the
totalCharactersvariable from your character counting step - Split the text into individual words using the
split()method with a space character as the delimiter - Store the resulting list of words in a variable called
wordList - Display the word splitting analysis results in the exact format shown below
- Show the original text, character count, and the list of words with proper formatting
For example, if your text analyzer was set up with "Hello world!", your program should output:
Text Analysis Tool - Word Splitting
====================================
Analyzing text: "Hello world!"
Total characters: 12
====================================
Word Splitting Results:
Words found: [Hello, world!]
====================================
Word splitting completed successfully.If your text analyzer was set up with "The quick brown fox", your program should output:
Text Analysis Tool - Word Splitting
====================================
Analyzing text: "The quick brown fox"
Total characters: 19
====================================
Word Splitting Results:
Words found: [The, quick, brown, fox]
====================================
Word splitting completed successfully.If your text analyzer was set up with "Programming is fun!", your program should output:
Text Analysis Tool - Word Splitting
====================================
Analyzing text: "Programming is fun!"
Total characters: 19
====================================
Word Splitting Results:
Words found: [Programming, is, fun!]
====================================
Word splitting completed successfully.Your program should use the split(' ') method on the string to break it into individual words separated by spaces. The resulting list will contain each word as a separate string element, which will be used for word counting and analysis in the next lessons. Note that punctuation marks will remain attached to the words they appear with.
Try it yourself
import 'dart:io';
void main() {
String? textToAnalyze = stdin.readLineSync();
int totalCharacters = textToAnalyze!.length;
print('Text Analysis Tool - Character Count');
print('====================================');
print('Analyzing text: "$textToAnalyze"');
print('====================================');
print('Character Analysis Results:');
print('Total characters (including spaces): $totalCharacters');
print('====================================');
print('Character counting 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