Chapter 22: Problem 29
Write a program that reads a series of strings and prints only those strings that end with the letters "ED."
Short Answer
Expert verified
Initialize input reading, filter strings with endswith('ED'), and print matching strings.
Step by step solution
01
Initialize the Program
Begin by setting up your program environment. If you're using Python, for example, open your Python IDE or terminal and start with the standard `if __name__ == "__main__":` structure to ensure your code runs as a script.
02
Input Reading Setup
Create a mechanism to read multiple strings from the user. You can use a simple `while` loop to keep reading strings until a certain condition is met, such as a specific termination word being entered (e.g., 'STOP'). Alternatively, you can prompt the user to enter a fixed number of strings.
03
Filter Strings
For each string read from the user, check if it ends with 'ED'. You can use Python's `str.endswith()` method which returns `True` if the string ends with the specified suffix and `False` otherwise. E.g., `if string.endswith('ED'):`.
04
Output the Result
If the check in Step 3 is `True`, print the string. Use a simple `print()` statement to display strings that end with 'ED' to the user.
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
String Manipulation
In programming, string manipulation is an essential skill, particularly in languages like C++. String manipulation involves modifying, parsing, or analyzing strings of text via code. In our exercise, we use string manipulation to check for a specific pattern, specifically whether a string ends with 'ED'.
To achieve this in a C++ program, you can use methods that access parts of the string:
To achieve this in a C++ program, you can use methods that access parts of the string:
substr()
: This function extracts a substring from a string. It's useful if you need to check specific segments of larger strings.compare()
: This function is ideal for checking whether a substring matches a certain criterion, such as ending with 'ED'. You could extract the last two characters usingsubstr()
and then compare them.
Conditional Statements
Conditional statements, like
In C++, the syntax for conditional statements generally involves using the
if
statements, are decision-making structures in programming. They allow your program to execute different actions based on certain conditions. In our task of checking if strings end with 'ED', conditional statements play a crucial role.In C++, the syntax for conditional statements generally involves using the
if
keyword followed by a condition in parentheses. If the condition evaluates to true, the code inside the curly braces runs. For our string-checking example, you might write:if (myString.ends_with("ED"))
: Using this C++20 feature, you can directly check if a string ends with another string. It's a clean and straightforward approach.- Older C++ versions: Could use
substr()
along with==
operator: if (myString.substr(myString.size() - 2) == "ED")
: Extracts the last two characters and compares them to 'ED'.
Loops
Loops are fundamental to programming because they allow code to be executed repeatedly based on a predefined condition. In our task, we want to read multiple strings until a user-defined condition ends the loop.
In C++, you commonly use
In C++, you commonly use
while
loops or for
loops. A while
loop is particularly useful for this task when you don't know how many iterations are necessary in advance. Here's a simple example:while (getline(cin, myString) && myString != "STOP")
: This keeps reading strings until the user inputs "STOP". This is a practical approach when you're unsure of the number of strings.
for
loop, if you specifically know the number of strings to read from the user, such as reading a fixed number of strings:for (int i = 0; i < n; i++)
: If you know the exact number of iterations you need.
for
or while
loops, they form the backbone for repeated task execution in programming.