B. Strings and Character Arrays A C++ string and an array of characters have similar behavior...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
B. Strings and Character Arrays A C++ string and an array of characters have similar behavior and can be similarly processed. In fact, a C++ string is implemented as an array in the libraries, but there are some differences as a string is an object (containing member variables and member functions) while a character array (or c-string) is simply an array of characters. To show how we can process a character array (also called a c-string) and see some of these differences, let us write a program that reverses a user-entered string once character at a time into a c-string and then compares the two to see if they are the same, meaning that the user-entered word is a palindrome. Use the following partial program as a start and complete the following three commented portions: • You can create the reverse c-string of the original string by processing the string in the usual manner (i.e., using a for loop to the end of the string) and then copying letter-by-letter, starting with the last letter of the regular string being assigned to the first letter of the character array. Inside the loop, this can be done with a simple assignment statement: assigning the letter from the string, starting with the last letter given by the rev variable (that has already been initialized to be the last letter of the string for you) to the first letter in the character array (the control variable i in the for loop). Then, be sure to decrement the rev variable so that it points to the next to last character in the string, and so forth. • All c-strings must have a null terminator '\0' appended as the last character to make sure they work properly. Assign the null terminator to index following the last character copied from the string. • Since we now have the original string and its reverse, fill in the if statement to perform a simple comparison of the original string and the reversed c- string to see if the string is a palindrome. Unfortunately, strings and c-strings are not directly comparable. To compare them, we must first convert the original string to a c-string using c_str () with the dot operator (e.g., for a string str, this can be done with str.c_str (). #include <iostream> #include <cstring> using namespace std; int main() { } string my_str; char reverse[21]; int i, rev; cout <<"Enter a string up to 20 characters long: "; cin >> my_str; rev = my_str.length () - 1; // create the reverse c-string of the original string // assign null character to last character in c-string if ( // write Boolean condition to compare c-strings) { cout << my_str << " is a palindrome" << endl; } else { } cout <<my_str << " and " << reverse << " are not the same << endl; return 0; Complete the requested changes, and save the file as Lab6B.cpp, making sure to compile and run the program to verify that it produces the correct results. For example, "racecar" is a palindrome. Note that you will submit this file to Canvas. B. Strings and Character Arrays A C++ string and an array of characters have similar behavior and can be similarly processed. In fact, a C++ string is implemented as an array in the libraries, but there are some differences as a string is an object (containing member variables and member functions) while a character array (or c-string) is simply an array of characters. To show how we can process a character array (also called a c-string) and see some of these differences, let us write a program that reverses a user-entered string once character at a time into a c-string and then compares the two to see if they are the same, meaning that the user-entered word is a palindrome. Use the following partial program as a start and complete the following three commented portions: • You can create the reverse c-string of the original string by processing the string in the usual manner (i.e., using a for loop to the end of the string) and then copying letter-by-letter, starting with the last letter of the regular string being assigned to the first letter of the character array. Inside the loop, this can be done with a simple assignment statement: assigning the letter from the string, starting with the last letter given by the rev variable (that has already been initialized to be the last letter of the string for you) to the first letter in the character array (the control variable i in the for loop). Then, be sure to decrement the rev variable so that it points to the next to last character in the string, and so forth. • All c-strings must have a null terminator '\0' appended as the last character to make sure they work properly. Assign the null terminator to index following the last character copied from the string. • Since we now have the original string and its reverse, fill in the if statement to perform a simple comparison of the original string and the reversed c- string to see if the string is a palindrome. Unfortunately, strings and c-strings are not directly comparable. To compare them, we must first convert the original string to a c-string using c_str () with the dot operator (e.g., for a string str, this can be done with str.c_str (). #include <iostream> #include <cstring> using namespace std; int main() { } string my_str; char reverse[21]; int i, rev; cout <<"Enter a string up to 20 characters long: "; cin >> my_str; rev = my_str.length () - 1; // create the reverse c-string of the original string // assign null character to last character in c-string if ( // write Boolean condition to compare c-strings) { cout << my_str << " is a palindrome" << endl; } else { } cout <<my_str << " and " << reverse << " are not the same << endl; return 0; Complete the requested changes, and save the file as Lab6B.cpp, making sure to compile and run the program to verify that it produces the correct results. For example, "racecar" is a palindrome. Note that you will submit this file to Canvas.
Expert Answer:
Answer rating: 100% (QA)
Certainly Below is the completed C program with the requested changes cpp include include using name... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Irvin Racquet Company produces tennis racquets. The direct labour standard for each racquet is 0.50 hours at a cost of $22.00 per direct labour hour. During the month of April, Irvin Racquet Company...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
For this prompt, consider your academic goals, including (but not limited to) such topics as how you plan to manage your time to fit in your studies; how you will build your skills, as needed; how...
-
Superior Gaming, a computer enhancement company, has three product lines: audio enhancers, video enhancers, and connection-speed accelerators. Common costs are allocated based on relative sales. A...
-
Jake owns a lawn maintenance company, and Luke owns a machine repair shop. For the month of July, the following transactions occurred. July 3 ...........Jake provides lawn services to Luke's repair...
-
(a) Why does the amplitude of the oscillation slowly decrease with time in the figure of last problem? (b) Why does the RHEED intensity increase at the end of the curve?
-
Bell Computers, Inc., balance sheet data at May 31, 2010, and June 30, 2010, follow: Requirement 1. Following are three situations about stockholders investments and dividends of the business during...
-
On San Martin Boulevard, embedded sensors kept track of the vehicle traffic count each hour for five weekdays, Monday through Friday between 6 a.m. and 8 p.m. (5 weeks 3 14 hours 5 70 observations)....
-
What is the fundamental challenge buyers face when purchasing under all unit discounts, compared to when they purchase under incremental discounts?
-
What are the triads using roman numerals and figured bass numbers for this excerpt? 30-3. Use Roman numerals and figured bass numbers to identify triads in these works. a. Schubert, "Death and the...
-
Consider an object that moves with constant acceleration of a=2.3 m/s. What is the position of the object after 3.0 seconds? You may complete the table or use an equation of motion. Give your answer...
-
Employment law 2) Robert is diagnosed with diabetes, and you overhear him telling his wife the condition is actually a blessing in disguise, because one he tells his boss he's disabled, he will have...
-
In a scene from a classic cartoon, a hungry coyote is chasing a roadrunner, as shown in the figure below. To aid in his quest, he has donned a pair of ACME brand rocket-propelled roller skates. The...
-
5.29 Given F = AB'D' + A'B+ A'C + CD. (a) Use a Karnaugh map to find the maxterm expression for F (express your answer in both decimal and algebric notation). (b) Use a Karnaugh map to find the...
-
A ship on the sea sends waves straight down into the seawater from the bottom of the ship. The signal reflects from the deep bottom bedrock and returns to the ship after 3.5 s. After the ship moves...
-
A Tale of Two Techs A few years ago, two medical technologists, Mike and Sam, who had the same educational background and experience, were placed into technical supervisory positions in different...
-
Q.2 If area bounded by the curve y = x and y = x, x 2 0 is revolved around x-axis then volume generated is 21 4
-
The power company must generate 100 kW in order to supply an industrial load with 94 kW through a transmission line with 0.09 resistance. If the load power factor is 0.83 lagging, find the...
-
Redo Listing 7.7, modifying the three array-handling functions to each use two pointer parameters to represent a range. The fill_array() function, instead of returning the actual number of items...
-
Redo the example shown in Listing 12.12, using the STL queue template class instead of the Queue class described in Chapter 12. Listing 12.12 bank.cpp // bank.cpp -- using the Queue interface //...
-
Here is a structure declaration: struct box { char maker[40]; float height; float width; float length; float volume; }; a. Write a function that passes a box structure by value and that displays the...
-
What is a wave equation? What is a traveling-wave solution?
-
Fill in the Blank. In the wave-solution \(w(x, t)=w_{1}(x-c t)+w_{2}(x+c t)\), the first term represents the wave that propagates in the __________ directions of \(x\). conditions
-
Find the time it takes for a transverse wave to travel along a transmission line from one tower to another one \(300 \mathrm{~m}\) away. Assume the horizontal component of the cable tension as 30,000...
Study smarter with the SolutionInn App