Chapter 18: Problem 9
Write a program that reads in several strings and prints only those ending in "r" or "ay". Only lowercase letters should be considered.
Short Answer
Expert verified
Use string manipulation to filter strings ending with 'r' or 'ay', then print them.
Step by step solution
01
Initialize the Program
Start your program by initializing the necessary variables. You will need a list to store the input strings and another list to keep track of the strings that meet the criteria.
02
Input Strings
Ask the user to input strings one by one. Continue accepting inputs until the user decides to stop, typically implied by a specific indicator like an empty input.
03
Check Each String
Iterate through each input string to determine if it meets the criteria. Specifically, check if a string ends with 'r' or 'ay'. This can be accomplished using string slicing and the endswith method.
04
Collect Valid Strings
For each string that meets the criteria, append it to the list of valid strings initialized earlier. This builds your collection of output-ready strings.
05
Output Results
Finally, iterate through the list of valid strings and print each one to the console. This completes the requirements of the exercise.
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
String manipulation is a fundamental concept in programming, especially in C++. It involves performing operations on strings in order to modify, analyze, or retrieve information.
In this exercise, we focus on checking the end of each string to match specific patterns. One of the primary operations used is **string slicing**. C++ doesn't directly support slicing like some other languages, but we can achieve similar functionality using methods and functions.
In this exercise, we focus on checking the end of each string to match specific patterns. One of the primary operations used is **string slicing**. C++ doesn't directly support slicing like some other languages, but we can achieve similar functionality using methods and functions.
- To determine the last few characters of a string, you can use methods like `substr`. This allows you to extract parts of the string efficiently.
- Similarly, the method `endswith`, although not available natively in standard C++, can be mimicked using `compare`, combining string length adjustments.
- For "r", check the last character of the string.
- For "ay", check the last two characters.
Control Structures
Control structures are a core part of C++ that allow developers to dictate the flow of a program. In this exercise, we make use of **loops** and **conditionals** to manage input and output processes.
The main control structure at play is the **loop**. We use loops to continually prompt users for input until they decide to stop.
- Typically, a `while` loop is used until a unique exit condition occurs, like an empty input.
- This loop efficiently gathers all the necessary strings, allowing for dynamic user interaction.
Conditional Statements
Conditional statements are essential in C++ for decision-making processes. They enable the execution of different code blocks based on certain conditions.
For this exercise, conditional statements are primarily used to check if each string ends with the desired characters, "r" or "ay".
The standard tool for this kind of check is the **if statement**. It allows you to test conditions and then run specific code if those conditions are true:
- The statement checks the last character or characters of the string.
- If the string ends with "r" or "ay", it proceeds to store the string in the "valid" list.