strlen, strcpy, strcat
Core string functions.
strlen, strcpy, strcat is a free C Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
Strings in C
In C, a string is just an array of char that ends with a special null terminator character '\0'.
The functions in <string.h> rely on this terminator to know where a string ends. Without it, they read past the end of your array.
#include <stdio.h>
int main(void) {
char name[] = "Alice";
printf("%s\n", name);
printf("Bytes used: %lu\n", sizeof(name));
return 0;
}Including string.h
To use string functions you must include the header:
#include <string.h>
This gives you strlen, strcpy, strcat, strcmp and more.
#include <stdio.h>
#include <string.h>
int main(void) {
char greeting[] = "Hello";
printf("Length is %lu\n", strlen(greeting));
return 0;
}strlen: counting characters
strlen returns the number of characters before the null terminator. It does not count '\0' itself.
So strlen("cat") returns 3, even though the array holds 4 bytes.
#include <stdio.h>
#include <string.h>
int main(void) {
char word[] = "cat";
printf("strlen = %lu\n", strlen(word));
printf("sizeof = %lu\n", sizeof(word));
return 0;
}strlen is O(n)
Because C strings have no stored length, strlen must walk through every character until it finds '\0'.
This makes it an O(n) operation. Avoid calling it repeatedly inside a loop condition for large strings.
#include <stdio.h>
#include <string.h>
int main(void) {
char msg[] = "programming";
size_t len = strlen(msg);
for (size_t i = 0; i < len; i++) {
printf("%c", msg[i]);
}
printf("\n");
return 0;
}strcpy: copying strings
You cannot copy strings with =. Use strcpy(dest, src) which copies src into dest, including the null terminator.
The destination must be large enough to hold the source plus the terminator.
#include <stdio.h>
#include <string.h>
int main(void) {
char src[] = "Hello";
char dest[20];
strcpy(dest, src);
printf("%s\n", dest);
return 0;
}strcpy buffer overflow danger
If dest is too small, strcpy writes past the buffer, corrupting memory. This is a classic security bug.
Prefer strncpy(dest, src, n) which copies at most n bytes. Note: strncpy may not null-terminate if src is too long.
#include <stdio.h>
#include <string.h>
int main(void) {
char src[] = "LongString";
char dest[5];
strncpy(dest, src, 4);
dest[4] = '\0';
printf("%s\n", dest);
return 0;
}strcat: joining strings
strcat(dest, src) appends src to the end of dest. It starts writing at dest's null terminator and adds a new one at the end.
dest must already be a valid string with enough free space.
#include <stdio.h>
#include <string.h>
int main(void) {
char buffer[20] = "Hello, ";
strcat(buffer, "World");
printf("%s\n", buffer);
return 0;
}Building strings step by step
You can chain strcpy and strcat to build a string piece by piece.
Always start with an initialized buffer so the first strcat finds a valid terminator.
#include <stdio.h>
#include <string.h>
int main(void) {
char full[50];
strcpy(full, "Mr. ");
strcat(full, "John ");
strcat(full, "Smith");
printf("%s\n", full);
return 0;
}Safer concatenation with strncat
strncat(dest, src, n) appends at most n characters from src, then always adds a null terminator.
This helps prevent overflows when the source length is unknown.
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[16] = "Data: ";
char src[] = "1234567890";
strncat(dest, src, 5);
printf("%s\n", dest);
return 0;
}Return values
Both strcpy and strcat return a pointer to dest. This lets you nest calls, though it can hurt readability.
strlen returns a size_t, which is unsigned. Be careful comparing it with signed integers.
#include <stdio.h>
#include <string.h>
int main(void) {
char a[20] = "foo";
char b[] = "bar";
printf("%s\n", strcat(a, b));
return 0;
}Common allocation pitfall
A frequent mistake is forgetting the +1 for the null terminator when sizing a buffer.
For a string of length n you need n + 1 bytes. The combined length of two joined strings is strlen(a) + strlen(b) + 1.
#include <stdio.h>
#include <string.h>
int main(void) {
char a[] = "abc";
char b[] = "de";
size_t needed = strlen(a) + strlen(b) + 1;
printf("Need %lu bytes\n", needed);
return 0;
}Quick Check
Test your understanding of string lengths.
Recap
You learned the core string.h functions:
- strlen counts characters before
'\0'(O(n)). - strcpy copies a string; destination must be large enough.
- strcat appends to an existing string.
- Always reserve space for the null terminator, and prefer the
strn*variants for safety.
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “strlen, strcpy, strcat” lesson free?
Yes — the full text of “strlen, strcpy, strcat” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.
What will I learn in “strlen, strcpy, strcat”?
Core string functions. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “strlen, strcpy, strcat” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- strlen, strcpy, strcat
- strcmp and Comparison
- strtok and Tokenizing
- memcpy and memset