Counting Characters
Part of the Logic & Flow section of Coddy's Dart journey — lesson 52 of 65.
Challenge
EasyBuilding on your text analyzer foundation, now you'll implement the character counting functionality. This step will calculate the total number of characters in your text, including letters, spaces, punctuation, and any other symbols.
- Use the
textToAnalyzevariable from your previous setup - Calculate the total number of characters in the text using the
.lengthproperty - Store the character count in a variable called
totalCharacters - Display the character analysis results in the exact format shown below
- Show both the original text and the character count with proper formatting
For example, if your text analyzer was set up with "Hello world!", your program should output:
Text Analysis Tool - Character Count
====================================
Analyzing text: "Hello world!"
====================================
Character Analysis Results:
Total characters (including spaces): 12
====================================
Character counting completed successfully.If your text analyzer was set up with "The quick brown fox jumps over the lazy dog", your program should output:
Text Analysis Tool - Character Count
====================================
Analyzing text: "The quick brown fox jumps over the lazy dog"
====================================
Character Analysis Results:
Total characters (including spaces): 43
====================================
Character counting completed successfully.If your text analyzer was set up with "Programming is fun!", your program should output:
Text Analysis Tool - Character Count
====================================
Analyzing text: "Programming is fun!"
====================================
Character Analysis Results:
Total characters (including spaces): 19
====================================
Character counting completed successfully.Your program should use the .length property on the string to count all characters, including letters, numbers, spaces, punctuation marks, and any other symbols. This character count will be used as a foundation for more advanced text analysis in the upcoming lessons.
Try it yourself
import 'dart:io';
void main() {
// Read the input text
String? inputText = stdin.readLineSync();
// Create the textToAnalyze variable
String textToAnalyze = inputText ?? '';
// TODO: Write your code below to display the text analysis tool setup
print('Text Analysis Tool - Setup Complete');
print('====================================');
print('Original text loaded for analysis:');
print('"$textToAnalyze"');
print('====================================');
print('Text analyzer is ready to process your data!');
print('Setup 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