Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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:
  • 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 using substr() and then compare them.
Understanding these functions allows you to effectively manipulate and work with strings within your program.
Conditional Statements
Conditional statements, like 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'.
Conditional statements, therefore, help you decide what should happen next in your code based on whether the strings meet the specified conditions.
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 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.
An alternative is a 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.
Understanding loops is essential for efficiently processing multiple strings and making decisions based on user input. Whether through for or while loops, they form the backbone for repeated task execution in programming.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Write a program that inputs a line of text and a search string from the keyboard. Using function strstr, locate the first occurrence of the search string in the line of text, and assign the location to variable searchPtr of type char \(* .\) If the search string is found, print the remainder of the line of text beginning with the search string. Then use strstr again to locate the next occurrence of the search string in the line of text. If a second occurrence is found, print the remainder of the line of text beginning with the second occurrence. [Hint: The second call to strstr should contain the expression searchPtr +1 as its first argument.

Write a program that right-shifts an integer variable 4 bits. The program should print the integer in bits before and after the shift operation. Does your system place zeros or ones in the vacated bits?

Write a program that inputs an ASCII code and prints the corresponding character. Modify this program so that it generates all possible three-digit codes in the range 000255 and attempts to print the corresponding characters. What happens when this program is run?

Find the error in each of the following: a. Assume that struct Card has been defined as containing two pointers to type charnamely, face and suit. Also, the variable c has been declared to be of type Card, and the variable cPtr has been declared to be of type pointer to Card. Variable cPtr has been assigned the address of c. cout << *cPtr.face << endl; b. Assume that struct Card has been defined as containing two pointers to type charnamely, face and suit. Also, the array hearts[ 13 ] has been declared to be of type Card. The following statement should print the member face of element 10 of the array. cout << hearts.face << endl; c. struct Person { char lastName[ 15 ]; char firstName[ 15 ]; int age; } d. Assume that variable p has been declared as type Person and that variable c has been declared as type Card. p = c;

Write a single statement or a set of statements to accomplish each of the following: a. Define a structure called part containing int variable partNumber and char array partName, whose values may be as long as 25 characters. b. Define partptr to be a synonym for the type Part *. c. Use separate statements to declare variable a to be of type Part, array b [ 10 ] to be of type part and variable ptr to be of type pointer to Part. d. Read a part number and a part name from the keyboard into the members of variable a. e. Assign the member values of variable a to element three of array b. f. Assign the address of array b to the pointer variable ptr. g. Print the member values of element three of array b, using the variable ptr and the structure pointer operator to refer to the members.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free