PY
C

Python to C

10 lessons

Progress0%
1Variables & Types2Functions3Lists → Arrays4Memory Management5String Handling6Structs7Preprocessor8File I/O9Pointers and Manual Memory10Build System: Makefile
All Mirror Courses
PY
C
String Handling
MirrorLesson 5 of 10
Lesson 5

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.

Mirror Card
PY
From Python:

In Python, you're familiar with text processing in c.

C
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:

C
C 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:

PY
Python (What you know)
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")
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

Python strings are objects with methods; C strings are char* with string.h functions

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

Python len() → C strlen(); Python == → C strcmp()

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

Python f-strings → C snprintf with format specifiers

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

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.

PY
Python
n = len(s)
C
C
int n = strlen(s); // walks the array

2. snprintf vs f-strings

C's snprintf writes a formatted string into a buffer. Always pass the buffer size to prevent overflow.

PY
Python
msg = f"Hello, {name}! Age: {age}"
C
C
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.

PY
Python
s.upper(); s.find("x"); s.replace("a","b")
C
C
/* 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
Common Pitfall
Don't assume C works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in C to practice these concepts.
PreviousNext