0
0
C++programming~30 mins

String handling basics in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
String handling basics
📖 Scenario: You are creating a simple program to manage a list of fruit names. You will practice basic string handling in C++.
🎯 Goal: Build a program that stores fruit names, checks for a specific fruit, counts how many fruits have names longer than a certain length, and then prints the results.
📋 What You'll Learn
Create a vector of strings with exact fruit names
Create an integer variable for length threshold
Use a for loop to count fruits with names longer than the threshold
Print the count and whether a specific fruit is in the list
💡 Why This Matters
🌍 Real World
Managing lists of names or items is common in apps like shopping lists, contact managers, or inventory systems.
💼 Career
Basic string handling and loops are foundational skills for software developers working with user input, data processing, and text analysis.
Progress0 / 4 steps
1
Create the fruit list
Create a std::vector<std::string> called fruits with these exact entries: "apple", "banana", "cherry", "date", "elderberry".
C++
Need a hint?

Use std::vector<std::string> fruits = { ... }; to create the list.

2
Set the length threshold
Create an int variable called length_threshold and set it to 5.
C++
Need a hint?

Write int length_threshold = 5; to create the variable.

3
Count fruits longer than threshold
Create an int variable called count_long and set it to 0. Then use a for loop with variable fruit to iterate over fruits. Inside the loop, increase count_long by 1 if fruit.length() is greater than length_threshold.
C++
Need a hint?

Use for (const std::string& fruit : fruits) to loop over the fruits.

4
Print the results
Use std::cout to print the text "Number of fruits longer than 5: " followed by count_long. Then check if "banana" is in fruits using a for loop with variable fruit. If found, print "Banana is in the list.", otherwise print "Banana is not in the list.".
C++
Need a hint?

Use std::cout to print messages and a for loop to find "banana".