String Handling
Text processing in C
Introduction
In this lesson, you'll learn about string handling in C. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with text processing in c.
C has its own approach to text processing in c, which we'll explore step by step.
The C Way
Let's see how C handles this concept. Here's a typical example:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
char s[] = "Hello, World!";
int n = strlen(s); /* 13 */
/* Manual upper — no built-in */
char upper[100];
for (int i = 0; s[i]; i++)
upper[i] = toupper((unsigned char)s[i]);
upper[n] = '\0';
/* Substring — manual */
char sub[6];
strncpy(sub, s + 7, 5);
sub[5] = '\0'; /* "World" */
/* Format string */
char msg[100];
char *name = "Alice"; int age = 30;
snprintf(msg, sizeof(msg), "Name: %s, Age: %d", name, age);
/* Find */
char *found = strstr(s, "World"); /* pointer to "World" in s */
/* No replace — manual or strdup+strsep patterns */Comparing to Python
Here's how you might have written similar code in Python:
s = "Hello, World!"
n = len(s)
upper = s.upper()
lower = s.lower()
sub = s[7:12] # "World"
parts = s.split(",")
joined = " ".join(parts)
# f-string formatting
name = "Alice"
age = 30
msg = f"Name: {name}, Age: {age}"
# Find and replace
idx = s.find("World")
replaced = s.replace("World", "Python")You may be used to different syntax or behavior.
Python strings are objects with methods; C strings are char* with string.h functions
You may be used to different syntax or behavior.
Python len() → C strlen(); Python == → C strcmp()
You may be used to different syntax or behavior.
Python f-strings → C snprintf with format specifiers
You may be used to different syntax or behavior.
Python string operations return new strings; C needs manual buffers
Step-by-Step Breakdown
1. strlen vs len
strlen walks the char array until it finds the null terminator. len() in Python is O(1) — stored with the object.
n = len(s)int n = strlen(s); // walks the array2. snprintf vs f-strings
C's snprintf writes a formatted string into a buffer. Always pass the buffer size to prevent overflow.
msg = f"Hello, {name}! Age: {age}"char msg[100];
snprintf(msg, sizeof(msg), "Hello, %s! Age: %d", name, age);3. No Method Calls
C strings have no methods. Use functions from <string.h>: strlen, strcpy, strncpy, strcmp, strcat, strstr.
s.upper(); s.find("x"); s.replace("a","b")/* upper: manual loop with toupper() */
/* find: strstr(s, "x") */
/* replace: no built-in — write your own or use regex */Common Mistakes
When coming from Python, developers often make these mistakes:
- Python strings are objects with methods; C strings are char* with string.h functions
- Python len() → C strlen(); Python == → C strcmp()
- Python f-strings → C snprintf with format specifiers
Key Takeaways
- C strings are char arrays + null terminator
- strlen vs len(), strcmp for equality, snprintf for formatting
- string.h provides basic operations; no method syntax
- Always manage buffer sizes to prevent overflow