Review
Enjoy the following last-minute extra practice problems! We'll go over these on Saturday morning if no one has any questions.
What will the following code print out?
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int MAX_LENGTH = 50;
char arr[][2][MAX_LENGTH] = {
{
"CSTRINGS",
"IN A"
},
{
"SEA OF",
"STRINGS"
}
};
int main () {
char* ptr1 = arr[0][0];
cout << ptr1 << endl; // #1
cout << ptr1[0] << endl; // #2
cout << (ptr1 + 3) << endl; // #3
char* ptr2 = arr[1][1];
ptr2[6] = 'Z';
cout << ptr2 << endl; // #4
*ptr1 = *ptr2;
cout << ptr1 << endl; // #5
cout << ptr2 << endl; // #6
}
Observe the two arrays arr and ans below. Your task is to turn each element of arr into the corresponding element of ans with the following restrictions:
You MUST use ONLY the cstring library's functions.
You MUST NOT assign to individual characters in the cstring using the [] operator.
You MUST NOT declare any additional variables.
You MUST NOT use ANY string literals like "string" in your answers.
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_LENGTH = 50;
char arr[][MAX_LENGTH] = {
"SEE",
"SAW",
"C",
"STRINGS"
};
char ans[][MAX_LENGTH] = {
"SEESAW",
"SAWINGS",
"SEESEE",
"RING"
};
int main () {
// -----------------------------------
// [!] Your code here!
// -----------------------------------
for (int i = 0; i < 4; i++) {
if (strcmp(arr[i], ans[i])) {
cout << "[X] FAILED: " << endl
<< "EXPECTED: " << ans[i] << endl
<< "GOT: " << arr[i];
return 1;
}
}
cout << "[!] You win CS31!" << endl;
}