Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ An array of C-strings can be treated much like a two dimensional array. Such a C-string can be indexed using two brackets([] []). Instructions:

C++
An array of C-strings can be treated much like a two dimensional array. Such a C-string can be indexed using two brackets([] []).
Instructions:
Open a new Visual Studio project. Open a new C++ source file. Create the following 2-dimensional char array:
char names[8][10] = {Robert,
James,
Wesley,
Marcus,
Randy,
Brandon,
Jeanette,
Darra};
Recall that in the above example, names[0][2] contains b and names[2][4] contains e.
Then, inside a main function, create a pair of loops to display the list of names in reverse order, and backwards character wise. The output should look like this:
arraD
ettenaeJ
nodnarB
ydnaR
sucraM
yelseW
semaJ
treboR
Lab Task 2 Serious Strings Version 2
(An exercise in using arrays of type string class)
Textbook Reference:
pp. 581-589
pp 418-425
In this task you will rewrite Task 1 using the string type (instead of C-strings) by using an array of strings to hold the names instead of the 2-dimensional array of C- Strings as you did in Task 1.
An array of strings can be treated much like a two-dimensional array. A string can be indexed using the bracket ([]) operator. The only difference with the string array is that each string does not have to be the same length, which is why the string class provides a length() function.
Instructions:
Open a new Visual Studio project. Open a new C++ source file. Create the following string array: (Remember to #include )
string names[8] = {Robert,
James,
Wesley,
Marcus,
Randy,
Brandon,
Jeanette,
Darra};
Then, inside a main function, create a pair of loops to display the list of names in reverse order, and backwards character wise. The output should look like this:
arraD
ettenaeJ
nodnarB
ydnaR
sucraM
yelseW
semaJ
treboR
Remember that if we have a string called str, str.length() will give us the length of the string. Also, if str contains STRING, str[0] will give us S, str[1] will give us T, and so on.
Lab Task 3 Replacing Substrings
(An exercise in processing the C-string type)
Textbook Reference:
pp. 554-580
Write a function named StringGame that replaces a substring with another substring. The function should accept three C-string arguments called string1, string2, string3. Note again that we are using C-Strings in this program, not String Class Objects. It should search string1 for an occurrence of string2. When it finds an occurrence of string2, it should replace it with string3. If string2 appears more than once in string1, only the first occurrence should be replaced. For example, if the three arguments are:
String1 the dog jumped over the fence
String2 the
String3 that
The function should return a string object with the value: that dog jumped over the fence
Now consider Example 2:
String1 the dog jumped over the fence
String2 jumped over
String3 climbed under
The function should return a string object with the value: the dog climbed under the fence
Demonstrate the function in a program with a main that allows the user to enter the three input strings, calls the function, and prints the contents of the returned string object.
Test the program either with a loop or by running it twice and turn in output for the following data sets:
String1 the dog jumped over the fence
String2 the
String3 that
String1 tell me where I can buy it
String2 where I
String3 when you
Hint: Create a string called resultString and build it by:
1. Putting in the part before the replacement (if there is any)
2. Putting in the replacement
3. Putting in the part after the replacement (if there is any).
Of course you will have to figure out how big this resultString needs to be by adding the lengths of the three components.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

More Books

Students also viewed these Databases questions