Question: The code in Exercise3.cpp was written in a way to be compatible with both C and C++. But this comes at a certain price, in

The code in Exercise3.cpp was written in a way to be compatible with both C and C++.

But this comes at a certain price, in that we have violated the recommendations for pure C++ code.

This is clearly defined by ISO 14882 in footnote 160:

The ".h" headers dump all their names into the global namespace, whereas the newer forms keep their names in namespace std. Therefore, the newer forms are the preferred forms for all uses except for C++ programs which are intended to be strictly compatible with C.

a. Replace both ".h" headers as shown below.

// #include #include // #include #include

Run your program and verify it still works. Now remove the angle brackets for both includes as in:

#include cstring

i. Does your program still work? Type Yes or No. Answer: _ _ _ _ _

(You will need to compile in C++).

What if you try quotes instead as in:

#include "cstring"

ii. Does your program still work? Type Yes or No. Answer: _ _ _ _ _

b. Let's work on that Apple Pie Problem!

Make a simple change to the line of code below, so that the output shows:

A is for: Apple Pie (and not just Apple

i. Replace the code below with the correct code.

ii. What does strlen() now return for variable a? Answer: _ _ _

char a[10] = { 'A', 'p', 'p', 'l', 'e', 0, 'P', 'i', 'e', 0 }; // extra 0

starting code:

// // Created by Robin Carr //

#include #include // Strings as character arrays. In C, a string is zero-terminated.

int main(void) {

printf("Strings as character arrays terminated by 0. "); printf("Here are some popular desserts. ");

char a[10] = { 'A', 'p', 'p', 'l', 'e', 0, 'P', 'i', 'e', 0 }; // extra 0 char b[10] = { 'B', 'i', 's', 'c', 'u', 'i', 't', 0}; char c[5] = { 'C', 'h', 'e', 'e', 's', 'e', 'c', 'a', 'k', 'e', 0 }; // Ack! Too small! char d[13] = {'D', 'a', 'n', 'i', 's', 'h', ' ', 'P', 'a', 's', 't', 'r', 'y'}; // no zero-termination char e[20] = {'E', 'c', 'l', 'a', 'i', 'r', 0};

printf("A is for: %s ", a); printf("B is for: %s ", b); printf("C is for: %s ", c); printf("D is for: %s ", d); printf("E is for: %s ", e);

// Add code here to print out the length of each character array. // Leave the extra 0 in a, and do not add the missing zero-termination for d. // the first is done for you as a sample.

printf(" Finding the length of each character array using sizeof()." );

size_t len = sizeof(a); printf(" The length of character array named a using sizeof() is %zu", len );

// Add code here to print out the length of each character array using strlen() // You need to include string.h // print out the length of all five char arrays using strlen() instead of sizeof(). // the first is done for you as a sample. printf(" Finding the length of each character array using strlen()." );

len = strlen(a); printf(" The length of character array named a using strlen() is %zu", len );

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!