Challenge - 5 Problems
String Master in C
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this C string code?
Consider this C code snippet that manipulates strings using pointers. What will be printed?
DSA C
char str[] = "hello"; char *p = str; p++; printf("%c\n", *p);
Attempts:
2 left
💡 Hint
Remember that p++ moves the pointer to the next character in the array.
✗ Incorrect
The pointer p initially points to 'h'. After p++, it points to 'e'. So, *p is 'e'.
❓ Predict Output
intermediate2:00remaining
What happens when you modify a string literal in C?
What will happen when running this C code?
DSA C
char *str = "hello"; str[0] = 'H'; printf("%s\n", str);
Attempts:
2 left
💡 Hint
String literals are stored in read-only memory in many systems.
✗ Incorrect
Modifying a string literal causes undefined behavior, often a segmentation fault at runtime.
🔧 Debug
advanced2:00remaining
Why does this string copy code cause a problem?
Examine this code that copies a string. What is the issue?
DSA C
char *src = "hello"; char dest[5]; strcpy(dest, src); printf("%s\n", dest);
Attempts:
2 left
💡 Hint
Remember that strings in C end with a null character '\0'.
✗ Incorrect
dest has size 5, but 'hello' plus '\0' needs 6 bytes, causing buffer overflow.
🧠 Conceptual
advanced2:00remaining
How are strings stored differently in C compared to higher-level languages?
Which statement best describes how strings work in C compared to languages like Python or Java?
Attempts:
2 left
💡 Hint
Think about how C strings know where they end.
✗ Incorrect
C strings are simple arrays ending with '\0'. Python and Java strings are objects that store length and other info.
🚀 Application
expert2:00remaining
What is the output after this pointer manipulation on a C string?
Given the code below, what will be printed?
DSA C
char str[] = "world"; char *p = str + 2; *(p + 1) = 'x'; printf("%s\n", str);
Attempts:
2 left
💡 Hint
Look carefully at which character is changed by *(p + 1) = 'x'.
✗ Incorrect
p points to 'r' (index 2). p+1 points to 'l' (index 3). Changing it to 'x' makes string 'worxd'.
