C program for string concatenation
C program for string concatenation
In C, string concatenation is the combination of two or more strings into one string. C does not have a built-in string data type, but strings are represented as arrays of characters, terminated by a null character ('\0').
To concatenate strings in C, you have several options:
1.String Concatenation Using a Loop in C
If you want to concatenate strings in C without using the strcat() function, you can achieve it by using a loop to copy the characters from one string to another until you reach the null character. Here's an example:
#include<stdio.h>
void concatenateStrings(char dest[],const char src[]){
int i=0;
int j=0;
while(dest[i]!='\0'){
i++;
}
while(src[j]!='\0') {
dest[i]= src[j];
i++;
j++;
}
dest[i]='\0';
}
void main(){
char str1[50]="Hello";
char str2[]=" World!";
concatenateStrings(str1,str2);
printf("Concatenated string:%s\n",str1);
}
Output:
![C program for string concatenation](https://static.tutorialandexample.com/c/c-program-for-string-concatenation1.png)
In this example, we have a function concatenateStrings that takes two arguments: the destination string (dest) and the source string (src).
The function loops over the destination string until it reaches the null character, indicating the end of the string. Then, it copies each character from the source string into the destination string starting from the position after the null character. Finally, it adds a null character at the end of the concatenated string to ensure proper termination.
The main() function demonstrates the usage of the concatenateStrings function. It initializes two character arrays, str1 and str2, with the strings "Hello" and " World!" respectively. Then, it calls the concatenateStrings function, passing str1 as the destination string and str2 as the source string. Finally, it prints the concatenated string using printf().
By using a loop to copy the characters, you can concatenate strings dynamically at runtime without relying on library functions. Just make sure that the destination string has enough memory allocated to accommodate the concatenated result and ensure proper null termination.
2.String Concatenation Using Recursion in C
In C, you can concatenate strings using recursion by defining a function that recursively appends characters from the source string to the destination string until the end of the source string is reached. Here's an example:
#include<stdio.h>
void concatenateStrings(char dest[],const char src[],int destIndex,int srcIndex){
if (src[srcIndex]=='\0'){
return;
}
dest[destIndex]=src[srcIndex];
concatenateStrings(dest,src,destIndex+1,srcIndex+1);
if(srcIndex==0){
dest[destIndex+1]='\0';
}
}
int main() {
char str1[50] = "Hello";
char str2[] = " World!";
concatenateStrings(str1, str2, 5, 0);
printf("Concatenated string: %s\n", str1);
return 0;
}
Output:
![C program for string concatenation](https://static.tutorialandexample.com/c/c-program-for-string-concatenation2.png)
In this example, we define the concatenateStrings function that takes the destination string (dest), source string (src), the index of the destination string (destIndex), and the index of the source string (srcIndex) as parameters.
The function recursively concatenates characters from the source string to the destination string. It checks if the current character in the source string (src[srcIndex]) is the null character ('\0'). If so, it returns, indicating the end of the source string has been reached and the concatenation is complete.
If the end of the source string is not reached, it appends the current character from the source string to the destination string (dest[destIndex] = src[srcIndex]). Then, it calls the concatenateStrings function recursively, incrementing both the destination and source indices (destIndex + 1 and srcIndex + 1).
After the recursion completes and all characters from the source string are appended to the destination string, it checks if the recursion is at the initial level (i.e., srcIndex == 0). If so, it adds a null character at the end of the concatenated string (dest[destIndex + 1] = '\0').
The main() function demonstrates the usage of the concatenateStrings function. It initializes two character arrays, str1 and str2, with the strings "Hello" and " World!" respectively.
It then calls the concatenateStrings function, passing str1, str2, the starting index of str1 (5, corresponding to the end of "Hello"), and the starting index of str2 (0). Finally, it prints the concatenated string using printf().
Using recursion to concatenate strings can be useful in scenarios where you need a recursive solution or want to understand recursive function calls better. However, keep in mind that recursion might have performance implications compared to iterative approaches in some cases.
3.string concatination using str() in c
In C, there is no built-in str() function for string concatenation. However, you can achieve string concatenation using various methods, such as the strcat() function from the standard library <string.h>.
Here's an example of string concatenation using strcat():
#include<stdio.h>
#include<string.h>
void main(){
char str1[50]="Hello";
char str2[]=" World!";
strcat(str1,str2);
printf("Concatenated string: %s\n",str1);
}
Output:
![C program for string concatenation](https://static.tutorialandexample.com/c/c-program-for-string-concatenation3.png)
In this example, we include the <string.h> header file to access the strcat() function. The strcat() function takes two arguments: the destination string (str1) and the source string (str2). It appends the content of the source string to the destination string. After the concatenation, str1 contains the concatenated result.
Note that the destination string (str1 in this case) should have enough memory allocated to accommodate the concatenated result. Also, ensure that the destination string is properly null-terminated to avoid buffer overflows or undefined behavior.
Using strcat() provides a convenient way to concatenate strings in C, leveraging the standard library functions.