Completion Complete the code snippets below by filling in each of the blanks. Th
ID: 3581251 • Letter: C
Question
Completion
Complete the code snippets below by filling in each of the blanks. The length of the blank does not indicate anything about the contents. Each blank may require more than one word or symbol to complete the program correctly. You may assume there are no errors in the code, and that is works as intended.
int main()
{
// create variables
_______________ var2;
int size;
char letter;
cout << "How long is your string? ";
cin >> size;
// allocate memory for string of that length
var2 = ___________ ____________ [___________];
// populate the string with letters
for (int i = 0; _________________; i++) {
cout << "Please enter a letter: ";
cin >> letter;
_________________ = letter;
}
var2[__________] = '';
cout << "Your string says '" << var2 << "'"
<< endl;
// free memory
_____________________;
return 0;
}
Explanation / Answer
Please let me know in case of any issue.
int main()
{
// create variables
char* var2;
int size;
char letter;
cout << "How long is your string? ";
cin >> size;
// allocate memory for string of that length
var2 = new char[size+1]; // one more length to store '' character
// populate the string with letters
for (int i = 0; i < size; i++) {
cout << "Please enter a letter: ";
cin >> letter;
var2[i] = letter;
}
var2[size] = '';
cout << "Your string says '" << var2 << "'"
<< endl;
// free memory
delete [] var2;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.