Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the string stored in name.
C
#include <stdio.h> int main() { char name[20] = "Alice"; printf([1], name); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d or %c instead of %s for strings.
Forgetting to add quotes around the format specifier.
✗ Incorrect
Use "%s" in printf to print a string variable.
2fill in blank
mediumComplete the code to read a string input from the user into the array input.
C
#include <stdio.h> int main() { char input[50]; printf("Enter your name: "); scanf([1], input); printf("Hello, %s!\n", input); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d or %c instead of %s in scanf.
Adding & before the array name when reading strings.
✗ Incorrect
Use "%s" in scanf to read a string input.
3fill in blank
hardFix the error in the code to correctly read a string with spaces from the user.
C
#include <stdio.h> int main() { char fullName[100]; printf("Enter your full name: "); fgets([1], 100, stdin); printf("Hello, %s", fullName); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using & before the array name with fgets.
Using array indexing like fullName[100] which is out of bounds.
✗ Incorrect
fgets requires the array name without & to read the string including spaces.
4fill in blank
hardFill both blanks to create a string copy using strcpy.
C
#include <stdio.h> #include <string.h> int main() { char source[] = "Hello"; char destination[20]; [1](destination, [2]); printf("Copied string: %s\n", destination); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping source and destination arguments.
Using strncpy without specifying length.
✗ Incorrect
Use strcpy(destination, source) to copy the string from source to destination.
5fill in blank
hardFill all three blanks to concatenate two strings safely using strncat.
C
#include <stdio.h> #include <string.h> int main() { char str1[20] = "Hello, "; char str2[] = "World!"; [1](str1, [2], [3]); printf("Concatenated string: %s\n", str1); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect length value for strncat.
Swapping the order of arguments.
✗ Incorrect
Use strncat(str1, str2, strlen(str2)) to concatenate str2 to str1 safely.