Session 5: Working with Text and Strings
Your Name
- 5 minutes read - 904 wordsSession 5: Working with Text and Strings
Learn to work with text data, handle multi-word input, and manipulate strings to create sophisticated text-based programs.
🎯 Key Skills You'll Learn:
- String operations
- getline() for full text input
- Text processing
- String methods
🚀 Session Project:
Initials Extractor
Learning Objectives
- Understand string operations in C++
- Learn basic string manipulation
- Handle multi-word input
- Practice string concatenation
Warm-up (5 minutes)
Try this: ask a user for their full name using cin >> name. What happens if they enter “John Smith”? Why do you think only “John” gets stored?
New Concept Introduction (20 minutes)
String Basics Revisited
|
|
The Problem with cin >>
|
|
Solution: getline()
|
|
String Operations
|
|
Useful String Methods
|
|
Mixing cin >> and getline()
This can cause problems! Here’s the safe way:
|
|
Hands-on Practice (25 minutes)
Exercise 1: Name Formatter (10 minutes)
|
|
Exercise 2: Sentence Analyzer (15 minutes)
|
|
Challenge/Quiz (10 minutes)
Challenge: Initials Extractor Create a program that:
- Asks for the user’s full name (first and last name)
- Extracts and displays their initials
- Counts the total number of characters in their name (excluding spaces)
- Displays their name in all capital letters (research: look up
toupper()function)
Example:
- Input: “John Smith”
- Output:
- Initials: J.S.
- Name length (no spaces): 9
- Uppercase: JOHN SMITH
Validation Prompt
🤖 AI Validation Prompt
Copy this prompt and your code to ChatGPT or Claude for instant feedback:
```
I'm learning C++ and just completed Session 5. Here's my solution to the Initials Extractor challenge:
[PASTE YOUR CODE HERE]
The challenge was: Create a program that extracts initials, counts characters (excluding spaces), and converts names to uppercase.
Please check if my solution:
1. Correctly extracts and displays initials with periods
2. Properly counts characters excluding spaces
3. Uses appropriate string methods
4. Handles full name input correctly (spaces included)
5. Shows the name in uppercase
If there are issues, please explain what's wrong and give me a hint to fix it (don't give me the complete solution).
```
What You Learned
- ✅ String concatenation with +
- ✅ Getting full lines of input with getline()
- ✅ String methods: length(), substr(), find()
- ✅ Accessing individual characters with []
- ✅ Mixing cin and getline safely
🎉 Phase 1 Complete!
What You’ve Accomplished
After these 5 sessions, you now understand:
- ✅ Basic C++ program structure
- ✅ Input and output with
cinandcout - ✅ Variables and data types
- ✅ Arithmetic operations and math
- ✅ String manipulation and text processing
Phase 1 Final Project (Optional Weekend Challenge)
Mini Text Adventure Setup Create a program that:
- Asks for the player’s name and age
- Calculates a “power level” based on their age (age * 10 + 50)
- Asks for their favorite weapon (string)
- Displays a character sheet with all their information
This combines everything you’ve learned in Phase 1!
Ready for Phase 2?
Phase 2 will introduce you to making decisions in your programs with if statements and conditions. You’ll learn how to make your programs smart enough to respond differently based on what the user inputs!