Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 7 of 7
Practice & Quiz - Arrays & Strings
Consolidate your C array and string skills - 1D/2D arrays, null termination, string.h safety, and buffer overflow prevention.
Practical Exercise
Practical Exercise
/* strbuilder.c - safe string operations with snprintf */
#include <stdio.h>
#include <string.h>
#define BUF 128
int main(void) {
char out[BUF];
/* snprintf: always null-terminates, returns chars that WOULD be written */
int n = snprintf(out, sizeof(out),
"Name: %-8s | Age: %d | Score: %.2f", "Alice", 28, 98.5);
printf("%s
", out);
if (n >= (int)sizeof(out)) printf("WARNING: truncated!
");
/* strrchr: find filename in path */
const char *path = "/home/alice/projects/main.c";
const char *fname = strrchr(path, '/');
printf("File: %s
", fname ? fname + 1 : path);
/* strtok on a COPY (never on a literal!) */
char line[] = "GET /index.html HTTP/1.1";
printf("Method : %s
", strtok(line, " "));
printf("URL : %s
", strtok(NULL, " "));
printf("Proto : %s
", strtok(NULL, " "));
/* Safe strncpy pattern */
char dst[16];
strncpy(dst, "Hello, World!", sizeof(dst) - 1);
dst[sizeof(dst) - 1] = '