Strings in C – Arrays of Characters
Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.
I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.
Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.
Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.
Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.
Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.
I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀
In the previous lessons, you learned about:
Arrays
Pointers
How arrays and pointers are related
Now we will apply that knowledge to something very common in programming: strings.
In C, strings are not a special data type like in some other languages.
Instead, strings are simply arrays of characters.
Understanding this will help you work with text in C programs.
1. What Is a String in C?
A string in C is:
A sequence of characters stored in an array and terminated by a special character called the null terminator (
'\0').
Example:
char name[6] = "Hello";
This string contains:
H e l l o \0
There are 6 characters in memory:
5 letters
1 null terminator
The null terminator marks the end of the string.
2. Why the Null Terminator Is Important
The null character ('\0') tells C where the string ends.
Without it, C would keep reading memory until it accidentally finds a zero.
Example in memory:
Index Value
0 H
1 e
2 l
3 l
4 o
5 \0
That \0 is essential.
3. Declaring Strings
Method 1: Using a String Literal
char name[6] = "Hello";
C automatically adds the null terminator.
Method 2: Using Character List
char name[6] = {'H','e','l','l','o','\0'};
You must include '\0' yourself.
Method 3: Letting C Determine Size
char name[] = "Hello";
C automatically allocates the correct size.
4. Printing Strings
You can print strings using %s.
Example:
#include <stdio.h>
int main() {
char name[] = "Hello";
printf("%s\n", name);
return 0;
}
Output:
Hello
%s tells printf to print characters until it reaches '\0'.
5. Looping Through a String
Since strings are arrays, we can use loops.
Example:
#include <stdio.h>
int main() {
char word[] = "School";
for (int i = 0; word[i] != '\0'; i++) {
printf("%c\n", word[i]);
}
return 0;
}
This prints each character one by one.
The loop stops when it reaches the null terminator.
6. Common String Functions
C provides useful string functions in <string.h>.
1. strlen – String Length
strlen(word);
Returns number of characters excluding '\0'.
Example:
strlen("Hello") → 5
2. strcpy – Copy String
strcpy(dest, source);
Example:
char a[10];
strcpy(a, "Hello");
Copies "Hello" into a.
3. strcat – Join Strings
strcat(dest, source);
Example:
char a[20] = "Hello ";
strcat(a, "World");
Result:
Hello World
4. strcmp – Compare Strings
strcmp(a, b);
Returns:
0 → strings equal
<0 → first string smaller
0 → first string larger
Example:
strcmp("cat", "cat") → 0
7. Strings and Pointers
Because strings are arrays, they are closely related to pointers.
Example:
char *s = "Hello";
Here:
spoints to the first character of the string"Hello"is stored in memory
However, this string is usually read-only.
Trying to modify it may cause an error.
Example:
s[0] = 'Y'; // Dangerous
Instead, use a character array if you need to modify the string.
8. Example Program
#include <stdio.h>
#include <string.h>
int main() {
char name[20] = "C Programming";
printf("String: %s\n", name);
printf("Length: %lu\n", strlen(name));
return 0;
}
Output:
String: C Programming
Length: 13
9. Common Beginner Mistakes
❌ Forgetting the Null Terminator
If you manually create a string and forget '\0', functions may behave incorrectly.
❌ Array Too Small
char name[5] = "Hello"; // Wrong
You need space for \0.
Correct:
char name[6] = "Hello";
❌ Modifying String Literals
char *s = "Hello";
s[0] = 'J'; // unsafe
Use a character array instead.
10. Practice Exercises
Create a string
"Programming"and print each character using a loop.Ask the user for a name and print it.
Write a program that:
copies one string into another
prints both.
Compare two strings using
strcmp.
Final Thoughts
Strings in C are simply arrays of characters.
Key ideas to remember:
Strings end with
'\0'%sprints stringsStrings can be looped through like arrays
<string.h>provides helpful functions
Understanding strings is important because text processing appears in almost every program.
In the next lesson, we will step back and look at data structures, where arrays, pointers, and structures combine to organize complex data.