Given the declaration: char str1[ 21]; char str2[ 21]; a. Write a C++ statement
ID: 644620 • Letter: G
Question
Given the declaration:
char str1[ 21];
char str2[ 21];
a. Write a C++ statement that stores " Sunny Day" in str1.
b. Write a C++ statement that stores the length of str1 into the int variable length.
c. Write a C++ statement that copies your name into str2.
d. Write C++ code that outputs str1 if str1 is less than or equal to str2, and otherwise outputs str2.
Hint: You should to use the utility functions that comes in the <cstring> library;
unless you want to write your own C-string functions or code J
include comments to clearly show which part of code is answering which part above.
Explanation / Answer
// a. Write a C++ statement that stores " Sunny Day" in str1.
char str1[21] = "Sunny Day";
// b. Write a C++ statement that stores the length of str1 into the int variable length.
int arrLength = strlen(str1);
// c. Write a C++ statement that copies your name into str2.
char str2[21];
strcpy(str2, str1);
// d. Write C++ code that outputs str1 if str1 is less than or equal to str2, and otherwise outputs str2.
if (strlen(str1) <= strlen(str2))
cout << str1 << endl;
else
cout << str2 << endl;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.