Write a C program to concatenate two strings using inbuilt library function strcat(). How to use strcat() library function.
Example:
Input string1: I love
Input string2: CodeforWin
Output string: I love CodeforWin
Required knowledge
Basic C programming, Loop, StringConcatenation of strings
Concatenation of two strings is the process of joining them together to form a new string. Concatenation basically joins the second string after first string. Concatenation is sometimes also referred as binary addition of strings i.e. + operation.For example: Codefor + Win = CodeforWin
Program to concatenate two strings
/** * C program to concatenate two strings without using inbuilt library function strcat() */ #include <stdio.h> #define MAX_SIZE 100 //Maximum size of the string int main() { char text1[MAX_SIZE], text2[MAX_SIZE], output[MAX_SIZE]; int i, j; /* Reads the two strings from user */ printf("Enter first string: "); gets(text1); printf("Enter second string: "); gets(text2); /* Copying text1 to output string */ i=0; while(text1[i]!='\0') { output[i] = text1[i]; i++; } /* Copies text2 to output string */ j = 0; while(text2[j]!='\0') { output[i] = text2[j]; i++; j++; } //Make sure that the output is NULL terminated output[i] = '\0'; printf("\nFirst string = %s\n", text1); printf("Second string = %s\n", text2); printf("Final concatenated string = %s\n", output); return 0; }
Note: You can also use inbuilt string library function strcat(str1, str2) to concatenate two strings. Where str2 is appended after str1. This function is present in string.h header file.
Program to concatenate two strings using strcat()
/** * C program to concatenate two strings using strcat() */ #include <stdio.h> #include <string.h> #define MAX_SIZE 100 //Maximum size of the string int main() { char text1[MAX_SIZE], text2[MAX_SIZE], output[MAX_SIZE]; int i, j; /* Reads the two strings from user */ printf("Enter first string: "); gets(text1); printf("Enter second string: "); gets(text2); /* Copies text1 to output */ strcpy(output, text1); /* Concatenates output(i.e. text1) with text2 */ strcat(output, text2); printf("\nFirst string = %s\n", text1); printf("Second string = %s\n", text2); printf("Final concatenated string = %s\n", output); return 0; }
Output
Enter first string: I love
Enter second string: CodeforWin
First string = I love
Second string = CodeforWin
Final concatenated string = I love CodeforWin
Enter second string: CodeforWin
First string = I love
Second string = CodeforWin
Final concatenated string = I love CodeforWin
Happy coding ;)
You may also like
- String programming exercises and solution index..
- C program to find length of a string.
- C program to copy one string to another string.
- C program to convert uppercase string to lowercase string.
- C program to convert lowercase string to uppercase string
- C program to find reverse of a given string.
- C program to check whether a string is palindrome or not.
- C program to count number of vowels and consonants in a string.
- C program to find total number of words in a string.
- C program to find first occurrence of a character in the string.
- C program to remove first occurrence of a character from the string.
- C program to find frequency of each character in a string.