/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 25 Write a program that reads a ser... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

Write a program that reads a series of strings and prints only those strings that end with the letters "ED."

Short Answer

Expert verified
Set up a loop to read strings, use a string method to check if each ends with 'ED', and print those that do before terminating the program upon a user-defined condition.

Step by step solution

01

Setup and Input Reading

Start by setting up your programming environment in your preferred language. Write the code to accept multiple strings as input from the user. This could be done using a loop that continues to accept input until a specific termination condition is met, such as the user entering an empty string or a specific quit command.
02

String Evaluation

Within the loop that reads the strings, after each input, check if the current string ends with the letters 'ED'. Most programming languages provide a method or function to check the ending of a string. For example, in Python, you could use the 'endswith' method.
03

Printing Matching Strings

If the string meets the condition (it ends with 'ED'), print the string to the output. If it doesn't meet the condition, simply ignore the string and continue with the next iteration of the loop to read the next string.
04

Terminate the Program

Once the termination condition is met (user inputs an empty string or a quit command), exit the loop and terminate the program. Before terminating, you may also print a completion message or summary if desired.

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 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

String Handling in C++
Working with strings in C++ involves accessing characters, concatenating, comparing, and manipulating the content of text data. A fundamental aspect often utilized in string handling is checking the ending of a string for specific characters or substrates.

C++ does not have a built-in 'endswith' method as some other languages do, but it provides substrings and comparison methods to achieve the same result. For example, to check if a C++ string ends with 'ED', one could utilize the 'substr' method to extract the last two characters of the string and then compare it with 'ED'.

It is vital to handle potential errors such as accessing characters outside the bounds of the string. Protecting against these errors involves checking the string's length and ensuring substrings are taken within the string's limits. Additionally, C++'s 'string' class provides other useful methods for transforming and examining text, which are essential for effective string handling.
Conditional Statements in C++
Conditional statements are a critical component of programming, allowing for decision-making processes based on logical or comparative conditions. In C++, the 'if' statement is one of the primary conditional constructs used to control the flow of the program.

For the program described in the exercise, an 'if' statement would be used to verify if each read string ends with 'ED' before deciding whether to print it. The format of an 'if' statement is quite straightforward:
  • The 'if' keyword is followed by a condition enclosed in parentheses.
  • The body of the 'if' statement, enclosed in curly braces, specifies the actions to be taken if the condition is true.

Using an 'if' statement correctly is paramount to ensuring that a program behaves as intended. In our case, the success of identifying strings that end with 'ED' is dependent on the correct formulation and placement of the 'if' statement within the program's loop.
Looping Constructs in C++
Looping constructs in C++ provide a way to repeatedly execute a block of code while a particular condition remains true. The program in our exercise makes use of loops to continuously accept user input until a termination condition is met.

There are several types of loops in C++, but the 'while' loop and the 'for' loop are most commonly used. The choice of loop often depends on whether the number of iterations is known beforehand or determined at runtime. In this scenario, since the number of input strings is unknown, a 'while' loop could be an excellent choice to keep accepting input until the user decides to stop.

Understanding 'while' Loops

A 'while' loop checks its condition before executing the loop's body. If the condition is true, the statements within the loop's body are executed. This process repeats until the condition evaluates to false. Properly handling the loop's exit condition, such as detecting an empty string or a quit command, is crucial to prevent infinite loops, which can cause a program to hang.

Looping constructs are powerful tools that, when used judiciously, can manipulate data efficiently, like filtering strings based on their termination as described in our exercise.

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 uses function strncmp to compare two strings input by the user. The program should input the number of characters to compare. The program should state whether the first string is less than, equal to or greater than the second string.

Write a program that inputs a telephone number as a string in the form (555) \(555-5555 .\) The program should use function strtok to extract the area code as a token, the first three digits of the phone number as a token, and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed.

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.

The availability of computers with string-manipulation capabilities has resulted in some rather interesting approaches to analyzing the writings of great authors. Much attention has been focused on whether William Shakespeare ever lived. Some scholars believe there is substantial evidence that Francis Bacon, Christopher Marlowe or other authors actually penned the masterpieces attributed to Shakespeare. Researchers have used computers to find similarities in the writings of these authors. This exercise examines three methods for analyzing texts with a computer. Thousands of texts, including Shakespeare, are available online at www. gutenberg.org. a) Write a program that reads several lines of text from the keyboard and prints a table indicating the number of occurrences of each letter of the alphabet in the text. For example, the phrase To be, or not to be: that is the question: contains one "a," two "b's," no "c's," etc. b) Write a program that reads several lines of text and prints a table indicating the number of one-letter words, two-letter words, three-letter words, etc., appearing in the text. For example, the phrase Whether 'tis nobler in the mind to suffer contains the following word lengths and occurrences: c) Write a program that reads several lines of text and prints a table indicating the number of occurrences of each different word in the text. The first version of your program should include the words in the table in the same order in which they appear in the text. For example, the lines To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer contain the word "to" three times, the word "be" two times, the word "or" once, etc. A more interesting (and useful) printout should then be attempted in which the words are sorted alphabetically.

Write a program that inputs several lines of text and a search character and uses function strchr to determine the total number of occurrences of the character in the lines of text.

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.