Please fill in the blanks for C /* Print full name*/ #include #include __1__ scan_print_name(__2__ size, __3__ name[size]); // function header int main()
Please fill in the blanks for C
/* Print full name*/
#include<stdio.h>
#include<stdbool.h>
__1__ scan_print_name(__2__ size, __3__ name[size]); // function header
int main()
{
int len;
bool isDone = false; //default to false since it's not done without running
char enter; //this is to avoid the first getChar captures the enter from length
//Get name and redo them over and over till the name length is big enough
// isDone is our flag to know if we need to run again.
// isDone == true means we are done, and vice versa
do
{
printf("\nEnter your full name's length.");
printf("Make sure to count spaces and null character:");
scanf("%d%c", &len, &enter);
char fullName[len];
isDone = scan_print_name(len, fullName);
}while(!isDone);
return 0;
}
/*Get and print the full name.
Return a boolean to check if the length is correct or not.
We set the default to be true (assume user counts correctly the size)
Now, we need to save user input to a variable so we can check. There are 3 things to check:
- If the inputted char is a new line character.
Our string is done. User pressed Enter => done
2. If i is bigger than the last index => user didn't count correctly for size
i reaches the last index of the string, and it's not 'Enter',
meaning the size is not long enough
Set flag to false to run again in main when return
Get out of loop
3. None of the above, then it's a normal letter, save inputted char to the string
Both case 1 and 2 will get out of loop, and print the name.
*/
__4__ scan_print_name(__5__ size, __6__ name[size])
{
bool isComplete = true; //default flag to true
printf("Input your full name: ");
for(int i = 0; i <= __7__; i++)
{
__8__ curChar = __9__(); //call function to get a character
if(__10__ == __11__) __12__;//Case 1, get out of loop
if(i > size - __13__) //Case2
{
printf("Your inputted length is too short.We'll redo this after printing the incomplete name.\n");
isComplete = __14__; //update the flag
__15__; //get out of loop and print the name
}
__16__ = __17__; //case 3
}
//this is outside of loop
// which means that we need to add the null char
// to the last index of the string
__18__[size - __19__] = __20__;
//print back
printf("Your full name: __21__\n", __22__);
//return flag
__23__ __24__;
}
Step by step
Solved in 2 steps