Challenge - 5 Problems
String Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of character traversal with pointer increment
What is the output of this C code snippet that traverses a string using a pointer?
DSA C
char *str = "hello"; char *p = str; while (*p) { printf("%c-", *p); p++; } printf("null");
Attempts:
2 left
💡 Hint
Remember that the loop prints each character followed by a dash until it reaches the null character.
✗ Incorrect
The loop prints each character followed by a dash. After the loop ends, it prints 'null'. So the output is characters separated by dashes ending with 'null'.
❓ Predict Output
intermediate2:00remaining
Value of character after pointer arithmetic
Given the code below, what character is printed by the printf statement?
DSA C
char str[] = "world"; char *p = str + 2; printf("%c", *p);
Attempts:
2 left
💡 Hint
Pointer p points to the third character in the string (index 2).
✗ Incorrect
The string "world" has characters w(0), o(1), r(2), l(3), d(4). Pointer p points to index 2, which is 'r'.
🔧 Debug
advanced2:00remaining
Identify the error in string traversal loop
What error does this code produce when compiled and run?
DSA C
char *s = "test"; int i = 0; while (s[i] != '\0') { printf("%c", s[0]); i--; }
Attempts:
2 left
💡 Hint
Check how the index variable changes inside the loop.
✗ Incorrect
The index i is decremented inside the loop, so it goes negative and s[i] never reaches '\0'. This causes an infinite loop printing the first character repeatedly.
❓ Predict Output
advanced2:00remaining
Output of nested loops accessing string characters
What is the output of this code snippet?
DSA C
char str[] = "abc"; for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++) { printf("%c", str[j]); } printf("-"); }
Attempts:
2 left
💡 Hint
Inner loop prints characters from start up to current outer loop index.
✗ Incorrect
For i=0, prints 'a'; i=1 prints 'ab'; i=2 prints 'abc'. Each followed by a dash.
🧠 Conceptual
expert2:00remaining
Number of characters printed by pointer loop
How many characters are printed by this code snippet?
DSA C
char *p = "data"; int count = 0; while (*p++) { count++; } printf("%d", count);
Attempts:
2 left
💡 Hint
The loop counts characters until the null terminator is reached.
✗ Incorrect
The string "data" has 4 characters before the null terminator. The loop increments count for each character until it reaches '\0'.
