C Program to concatenate string without using strcat()
|In this C Programming example, we will implement the program to concatenate string without using strcat()
and print the output on the console.
1. What is String Concatenation?
String Concatenation is a process when one string is appended to the end of another string.
Example 1: Input: The first string: Codingeek The second string: .com Output: After the concatenation: Codingeek.com
Helpful topics to understand this program better are-
2. C Program to concatenate string without using strcat()
Let’s discuss the execution(kind of pseudocode) for the program to concatenate string without using strcat() in C.
- Initially, the program will prompt the user to enter two strings for the concatenation.
- Then we invoke the method
concatenateString()
. - In
void concatenateString()
,while
andfor
loops are used to iterate the length of the string and append the first string to the end of the other. - After the concatenation, the concatenated string is printed on the console.
Let us now implement the above execution of the program to concatenate two strings without using strcat() in C.
#include <stdio.h> char s1[100], s2[100], j; int length; length = 0; void concatenateString(){ while (s1[length] != '\0') { ++length; } for (j = 0; s2[j] != '\0'; ++j, ++length) { s1[length] = s2[j]; } s1[length] = '\0'; } int main() { printf("\nEnter the first string: "); scanf("%s",s1); printf("\nEnter the second string: "); scanf("%s",s2); concatenateString(); printf("After the concatenation: "); puts(s1); return 0; }
Note: The characters entered must be within the range declared in char[] data type and no spaces must be entered within the characters.
Output Enter the first string: coding Enter the second string: eek After the concatenation: codingeek
3. Conclusion
In this C Programming example, we have discussed what is String concatenation and how to concatenate string without using strcat() method.
Helpful Links
Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.
Also for the example C programs please refer to C Programming Examples.
All examples are hosted on Github.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?