Concatenating Two Strings in C (original) (raw)
Last Updated : 28 Nov, 2024
Concatenating two strings means appending one string at the end of another string. In this article, we will learn how to concatenate two strings in C.
The most straightforward method to concatenate two strings is by using**strcat()function. Let's take a look at an example:
C `
#include <stdio.h> #include <string.h>
int main() { char s1[] = "Hello "; char s2[] = "Geeks";
// Concatenate str2 to str1
strcat(s1, s2);
printf("%s\n", s1);
return 0;
}
`
The **strcat() function is specifically designed to concatenate two strings. It appends the second string (source string) to the end of the first string (destination string)
**Note: The destination string should have enough space to store the concatenated string. Otherwise, a segmentation fault may occur.
There are also a few other methods in C to concatenate two strings. Some of them are as follows:
Table of Content
Using sprintf()
The **sprintf() function is used to print / store a formatted string to a string buffer. It can be used for concatenating two strings by using the source string as formatted string and destination string as string buffer.
C `
#include <stdio.h>
int main() { char s1[20] = "Hello "; char s2[] = "Geeks";
// Concatenate str1 and str2 using sprintf
sprintf(s1 + strlen(s1), "%s", s2);
printf("%s\n", s1);
return 0;
}
`
Using memmove()
The **memmove() function can be used to copy the whole block of memory occupied by source string at the end of destination string effectively concatenating the two strings.
C `
#include <stdio.h> #include <string.h>
int main() { char s1[20] = "Hello "; char s2[] = "Geeks";
// Use memmove to concatenate str1 and str2
memmove(s1 + strlen(s1), s2, strlen(s2) + 1);
printf("%s\n", s1);
return 0;
}
`
**Note: The memcpy() function can also be used in place of memmove() if the source and destination string does not overlap.
Manually Using a Loop
Use a loop to copy each character of source string at the end of the destination string. The end of the destination string can be found by using strlen() function.
C `
#include <stdio.h> #include <string.h>
void concat(char s1[], char s2[]) {
// Go to the end of the string s1
s1 = s1 + strlen(s1);
// Copy characters from s2 to s1 using pointers
while (*s1++ = *s2++);
}
int main() {
char s1[50] = "Hello ";
char s2[] = "Geeks";
concat(s1, s2);
printf("%s\n", s1);
return 0;
}
`
Similar Reads
- String in Data Structure A string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut 3 min read
- Introduction to Strings - Data Structure and Algorithm Tutorials Strings are sequences of characters. The differences between a character array and a string are, a string is terminated with a special character ‘\0’ and strings are typically immutable in most of the programming languages like Java, Python and JavaScript. Below are some examples of strings:"geeks" 7 min read
- Applications, Advantages and Disadvantages of String The String data structure is the backbone of programming languages and the building blocks of communication. String data structures are one of the most fundamental and widely used tools in computer science and programming. They allow for the representation and manipulation of text and character sequ 6 min read
- Subsequence and Substring What is a Substring? A substring is a contiguous part of a string, i.e., a string inside another string. In general, for an string of size n, there are n*(n+1)/2 non-empty substrings. For example, Consider the string "geeks", There are 15 non-empty substrings. The subarrays are: g, ge, gee, geek, ge 6 min read
- Storage for Strings in C In C, a string can be referred to either using a character pointer or as a character array. Strings as character arrays C char str[4] = "GfG"; /*One extra for string terminator*/ /* OR */ char str[4] = {‘G’, ‘f’, ‘G’, '\0'}; /* '\0' is string terminator */ When strings are declared as character arra 5 min read