🚀 One Tiny String Tip That Speeds Up Your C Code

🚀 One Tiny String Tip That Speeds Up Your C Code

When I’m learning something new, I always ask:

How does this connect to something I already know?

Linking new ideas to familiar ones makes them stickier—and in programming, everything is connected if you look closer enough.

Today, I want to talk about strings vs arrays in C, especially through the lens of access, modify, and iterate—the three superpowers of data structures.


🧩 Strings vs Arrays: Key Differences at a Glance

Article content

🔍 Let’s Focus on: Access, Modify, and Iterate

Here’s our string for the rest of the article:

string s = "Hello";        

💡 In CS50, they simplify syntax for beginners by allowing you to write string s = "Hello";.

Under the hood, this is actually just:

char *s = "Hello";        

That means s is a pointer to the first character of a string literal.

Don't stress if this seems mysterious right now—we’ll dive deep into pointers and memory in Week 4. For now, just remember: a string in C is really just a pointer to a sequence of characters ending in '\0'

1. Access (Read)

printf("%c\n", s[1]);  // Outputs: 'e'        

Simple and fast! Just like an array—because a string is a character array.


2. Modify (Write)

s[2] = 'a';  // ❌ Crash alert: Segmentation fault!        

Why does this fail?

Because "Hello" is a string literal, stored in read-only memory. Trying to change it is like scribbling on a sign in a museum: C will crash your program.

💡 Want a modifiable string? Use an array instead:

char s[] = "Hello";  // ✅ You can modify this
s[2] = 'a';  // Now it's "Hallo"        

3. Iterate (Loop)

The most interesting part: let’s talk iteration.

for (int i = 0; i < strlen(s); i++) {
    printf("%c", s[i]);
}        

This works great—but… there’s a hidden trap.


❓ Mini Lesson: Why Store strlen(s) in a Variable?

Let’s say your string has 100,000 characters.

Big thanks to my mentor for helping me better understand the difference between these two approaches! 🙌 Take a look and see the impact it can make in your code.

// ❌ Slow
for (int i = 0; i < strlen(s); i++)

// ✅ Efficient
for (int i = 0, n = strlen(s); i < n; i++)        

But the first one is much slower.

Why? Because in C, strlen() doesn’t just return a saved value—it scans every character until it finds '\0'.

So if your string has 100,000 characters, then strlen() takes O(n), and doing that every iteration becomes O(n²)!

🔬 Let’s Test It for Real

Here's a simple program that measures the difference:

Thanked my mentor helped improve how different bewtween them, please take a look.

Article content

🧪 Output:

Article content

⚠️ The Takeaway:

The longer your string, the bigger the gap between efficient and inefficient loops.

With short strings (like "Hello"), you won’t notice. But with large data? Performance explodes.

📌 Final Takeaways

  • Strings are just arrays of characters ending in '\0'.
  • String literals are not modifiable—use a char[] if you want to change them.
  • When iterating, store strlen() in a variable—especially for long strings.
  • Tiny changes in logic = big gains in performance as scale increases.

📚 Reference: CS50 Week 2 Notes


💡 This is a key concept I’ve learned, and I believe it’s especially useful for anyone starting their programming journey.

If you have any insights, experiences, or practical tips related to this topic, feel free to share them in the comments — I’d love to learn from you, and others will too!

🧠 The more we share knowledge, the more we all grow together.

✨ If you find them helpful, a quick reaction or comment would truly make my day and keep me motivated to share more. Thank you! Stay tuned 🚀✨        

To view or add a comment, sign in

More articles by Miro Doan

Others also viewed

Explore content categories