Bird
0
0
DSA Cprogramming~20 mins

String Traversal and Character Access in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
Ah e l l o null
Bh-e-l-l-o-
Chello-null
Dh-e-l-l-o-null
Attempts:
2 left
💡 Hint
Remember that the loop prints each character followed by a dash until it reaches the null character.
Predict Output
intermediate
2: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);
Al
Br
Co
Dd
Attempts:
2 left
💡 Hint
Pointer p points to the third character in the string (index 2).
🔧 Debug
advanced
2: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--;
}
AInfinite loop printing 't'
BCompilation error
CPrints 'test' once and stops
DSegmentation fault
Attempts:
2 left
💡 Hint
Check how the index variable changes inside the loop.
Predict Output
advanced
2: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("-");
}
Aa-ab-abc-
Babc-abc-abc-
Ca-aa-aaa-
Da-bc-abc-
Attempts:
2 left
💡 Hint
Inner loop prints characters from start up to current outer loop index.
🧠 Conceptual
expert
2: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);
A3
B5
C4
D0
Attempts:
2 left
💡 Hint
The loop counts characters until the null terminator is reached.