String concatenation means joining two strings together into one. In C, the standard library provides the strcat()
function to handle this task easily. However, writing your own program to concatenate strings without using strcat()
is a very good practice. It helps you understand how strings are stored in memory and how characters are manipulated one by one.
In this tutorial, we will learn how to concatenate two strings without using strcat()
. We will look at different methods, including using loops, pointers, recursion, and even sprintf()
. Each example will be explained carefully so that you can understand not just the code, but also the logic behind it. By the end, you will be confident about handling strings manually in C.
Understanding the Problem
In C, a string is simply an array of characters ending with a null terminator (\0
). Concatenation means taking all the characters from the second string and adding them to the end of the first string, then making sure the final result also ends with a null terminator.
The challenge when not using strcat()
is that you must do this process manually. You need to find the length of the first string, move to that position, and then copy each character of the second string one by one. This makes you think about how memory works and how characters are stored in arrays.
Program 1: Concatenation Using a Loop
This method finds the end of the first string and then uses a loop to copy characters from the second string until it ends.
#include <stdio.h>
#include <string.h>
int main() {
char str1[200], str2[100];
int i = 0, j = 0;
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove trailing newline added by fgets (if present)
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
// Get the index where str1 ends
i = strlen(str1);
// Copy characters from str2 into str1 starting at the end
while (str2[j] != '\0') {
str1[i++] = str2[j++];
}
// Add null terminator at the end of the new string
str1[i] = '\0';
printf("Concatenated string: %s\n", str1);
return 0;
}
This approach is easy to follow because it simply walks through both strings and performs concatenation step by step.
Program 2: Concatenation Using Pointers
Pointers allow us to move through the strings directly without using array indexes.
#include <stdio.h>
#include <string.h>
int main() {
char str1[200], str2[100];
char *p, *q;
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove trailing newline added by fgets (if present)
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
// Move p to the end of str1
p = str1;
while (*p != '\0') {
p++;
}
// Copy str2 into str1
q = str2;
while (*q != '\0') {
*p = *q;
p++;
q++;
}
// Null terminate
*p = '\0';
printf("Concatenated string: %s\n", str1);
return 0;
}
Using pointers makes the code more compact and efficient, and it shows how characters are stored in memory.
Program 3: Concatenation Using Recursion
With recursion, the function moves to the end of the first string and then begins copying characters of the second string.
#include <stdio.h>
#include <string.h>
void concatenate(char *str1, char *str2) {
if (*str1 == '\0') {
while (*str2 != '\0') {
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0';
return;
}
concatenate(str1 + 1, str2);
}
int main() {
char str1[200], str2[100];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove trailing newline added by fgets (if present)
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
concatenate(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
Although recursion is not commonly used for string concatenation in real-world applications, it is an elegant method that helps you understand how recursive functions work.
Program 4: Concatenation Using sprintf()
sprintf()
is part of the standard library and is usually used for formatting strings, but it can also be used to concatenate two strings efficiently.
#include <stdio.h>
#include <string.h>
int main() {
char str1[200], str2[100];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove trailing newline added by fgets (if present)
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
sprintf(str1 + strlen(str1), "%s", str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
This method is very concise. It uses formatted output to append the second string at the end of the first one. However, you need to ensure there is enough buffer space in str1
to hold both strings safely.
Program 5: Concatenation Using memcpy()
The memcpy()
function can also be used to concatenate strings efficiently by copying the second string directly to the end of the first string. You must ensure the destination buffer has enough space and include the null terminator.
#include <stdio.h>
#include <string.h>
int main() {
char str1[200], str2[100];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove trailing newline added by fgets (if present)
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
// Concatenate using memcpy
size_t len1 = strlen(str1);
size_t len2 = strlen(str2);
memcpy(str1 + len1, str2, len2 + 1); // +1 to include null terminator
printf("Concatenated string: %s\n", str1);
return 0;
}
This method is efficient because it copies the bytes directly without formatting. The null terminator must be included manually to ensure the resulting string is valid. Unlike sprintf()
, no formatting is performed—just raw memory copy.
FAQs
1. Can I concatenate strings without extra memory?
Yes, as long as the destination string (usually the first one) is large enough to hold the result, you do not need extra memory.
2. Which method is most efficient?
The pointer method and sprintf()
are efficient and widely used. The loop method is also fine, though slightly more verbose.
3. Should recursion be used in real programs?
Recursion works, but it is not practical for string concatenation in production code. Loops and pointers are much faster and use less memory.
4. Is sprintf()
safe for concatenation?
It is powerful but can be unsafe if the buffer size is not managed carefully. If available, snprintf()
is safer because it limits the number of characters written.
Conclusion
Concatenating two strings without using strcat()
is a great exercise for understanding how strings really work in C. We have explored four methods: using loops, using pointers, using recursion, and using sprintf()
. Each method has its own strengths and teaches you something new about working with strings.
For most real-world tasks, the loop method and the pointer method are practical, while sprintf()
is useful when you are already working with formatted strings. Recursion is mostly for practice and learning. Mastering these approaches will give you a deeper confidence when handling strings in C.
References & Additional Resources
A curated collection of textbooks, tutorials, and documentation for learning string concatenation, pointers, and C string functions.
- Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language, 2nd Edition, Prentice Hall, 1988 – The definitive text covering strings, pointers, arrays, and core C programming concepts.
- GeeksforGeeks: Concatenation of Strings Without strcat – Demonstrates how to concatenate two strings manually in C, without using
strcat()
. - Tutorialspoint: C Strings – Overview of string declaration, initialization, and common operations in C.
- Cprogramming.com: Pointers in C – Beginner-friendly guide explaining pointers and their use in string and array manipulation.
- cplusplus.com: sprintf() Function – Reference for the
sprintf()
function for formatting and storing strings in C. - ISO C Standard Library – Official reference for standard C string functions, including concatenation, copying, and length calculation.