- Write a C program to concatenate two strings.
- Write a C program to join two strings..
Appending strings in c: We first take two string as input from user using gets function and store it in two character array. Now, we have to append first string at the end of second string. We can either use strcat function of string.h header file to concatenate strings or write our own function to append strings using pointers. Second string must be large enough to contain the concatenated resulting string. For example, if first string is “Tech” and second string is “CrashCourse” then on concatenating these two strings we get “TechCrashCourse”.
C program to concatenate two strings using strcat function
Append string in c: To use strcat function, we must include string.h header file in our program. Here is the declaration for strcat function.
char *strcat(char *destination, const char *source);
The strcat() function concatenates a source string to destination string and adds a null character at the end of destination. Source string is not modified in this operation. It returns concatenated destination string. Length of destination string must be more than sum of original length of both strings.
/* * C Program to concatenate two strings using strcpy function */ #include <stdio.h> #include <conio.h> #include <string.h> int main(){ char destination[100], source[100]; printf("Enter first string \n"); gets(destination); printf("Enter second string \n"); gets(source); /* * char *strcat(char *destination, const char *source) * Concatenates source at the end of destination */ strcat(destination, source); printf("Concatenated String: %s \n", destination); getch(); return 0; }
Program Output
Enter first string Tech Enter second string CrashCourse Concatenated String: TechCrashCourse
Write a program in c to concatenate two strings using pointers
How to append strings in c: In this program, we are using a user defined function ‘concatenateString’ to concatenate two strings. It takes source and destination string pointers as parameters and does input validation(neither source nor destination pointer should be NULL). It calculate length of destination string using strlen and then appends characters of source string into destination string by overwriting null character of destination string. At last it adds null character in destination string and returns it.
/* * C Program to concatenate two strings using pointer */ #include <stdio.h> #include <conio.h> #include <string.h> int getStringlength(char *inputString); char* concatenateString(char *firstString, char *secondString); int main(){ char destination[100], source[100]; printf("Enter first string \n"); gets(destination); printf("Enter second string \n"); gets(source); concatenateString(destination, source); printf("Concatenated String: %s \n", destination); getch(); return 0; } /* * Concatenates source at the end of destination string */ char* concatenateString(char *destination, char *source){ if(NULL == destination || NULL == source){ return NULL; } int index = 0, length = strlen(destination); while(source[index] != '\0'){ destination[length] = source[index]; length++; index++; } destination[length] = '\0'; return destination; }
Program Output
Enter first string Hello Enter second string World Concatenated String: HelloWorld