Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print each character of the string using a loop.
DSA C
char str[] = "hello"; for (int i = 0; i < [1]; i++) { printf("%c\n", str[i]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using sizeof(str) which includes the null terminator and size of array.
Using a fixed number without considering string length.
✗ Incorrect
strlen(str) returns the length of the string excluding the null character, so the loop prints all characters.
2fill in blank
mediumComplete the code to access the last character of the string.
DSA C
char str[] = "world"; char last_char = str[[1]]; printf("%c\n", last_char);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strlen(str) as index which is out of bounds.
Using sizeof(str) which includes null terminator.
✗ Incorrect
The last character is at index length - 1 because indexing starts at 0.
3fill in blank
hardFix the error in the code to correctly print characters until the null terminator.
DSA C
char str[] = "test"; int i = 0; while (str[[1]] != '\0') { printf("%c\n", str[i]); i++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed index like 0 or 1 in the condition causes infinite loop or wrong output.
Using strlen(str) inside the condition without updating index.
✗ Incorrect
The loop should check str[i] to stop at the null terminator correctly.
4fill in blank
hardFill both blanks to create a loop that prints characters only if they are vowels.
DSA C
char str[] = "example"; for (int i = 0; i < [1]; i++) { if (str[i] == [2]) { printf("%c\n", str[i]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop limit causing out of bounds.
Checking for a consonant instead of a vowel.
✗ Incorrect
Loop runs through the string length and prints characters only if they are 'e' (a vowel).
5fill in blank
hardFill all three blanks to create a loop that counts how many times 'l' appears in the string.
DSA C
char str[] = "hello world"; int count = 0; for (int i = 0; i < [1]; i++) { if (str[i] == [2]) { [3]; } } printf("%d\n", count);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong character in condition.
Not incrementing count inside if block.
✗ Incorrect
Loop runs through the string length, checks if character is 'l', and increments count.
