Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a string variable in C.
DSA C
char name[] = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes for strings
Not using quotes at all
✗ Incorrect
In C, strings are arrays of characters enclosed in double quotes. Single quotes are for single characters.
2fill in blank
mediumComplete the code to print the first character of the string.
DSA C
printf("%c", name[1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of brackets
Starting index at 1
✗ Incorrect
String indexing in C starts at 0, so name[0] gives the first character.
3fill in blank
hardFix the error in the code to copy one string to another.
DSA C
strcpy(dest, [1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing destination as source
Using & or * operators incorrectly
✗ Incorrect
The source string must be passed as a pointer to strcpy, not the destination or address operators.
4fill in blank
hardFill both blanks to declare and initialize a string pointer.
DSA C
char *ptr = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes for strings
Not using quotes at all
✗ Incorrect
A string pointer can be initialized to a string literal enclosed in double quotes.
5fill in blank
hardFill all three blanks to print the length of a string using strlen.
DSA C
int len = strlen([1]); printf("Length: %[2]\n", [3]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using %c instead of %d for printing length
Passing wrong variable to strlen
✗ Incorrect
strlen takes the string variable, printf uses %c for char, but for int length we use %d. Here %c is incorrect, correct is %d.
