Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a string in C.
DSA C
char str[] = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes for strings
Not enclosing the string in quotes
Adding a semicolon inside the quotes
✗ Incorrect
In C, strings are declared as arrays of characters enclosed in double quotes.
2fill in blank
mediumComplete the code to print the string using printf.
DSA C
printf([1], str); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using %c which prints only one character
Using %d or %f which are for numbers
✗ Incorrect
The %s format specifier is used to print strings in C.
3fill in blank
hardFix the error in copying a string using strcpy.
DSA C
char dest[20]; strcpy(dest, [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using destination as source
Using string literal instead of variable
✗ Incorrect
The second argument to strcpy should be the source string variable, not the destination or a string literal.
4fill in blank
hardFill both blanks to create a loop that prints each character of a string until the null character.
DSA C
int i = 0; while (str[i] [1] '\0') { printf("%c", str[i]); i[2]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of !=
Decrementing i instead of incrementing
✗ Incorrect
The loop continues while the character is not the null character '\0'. The index i is incremented each time.
5fill in blank
hardFill all three blanks to create a function that returns the length of a string.
DSA C
int string_length(char *str) {
int [1] = 0;
while (str[[2]] != [3]) {
length++;
}
return length;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names
Not checking for '\0' correctly
✗ Incorrect
We use a variable length initialized to 0, check characters until the null character '\0', and increment length.
