In this tutorial you will learn about the C Program to Concatenate Two Strings Without Using strcat and its application with practical example.
C Program to Concatenate Two Strings Without Using strcat
In this tutorial, we will learn to create a C program that will Concatenate Two Strings Without Using strcat in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in c programming.
Concatenate Two Strings Without Using strcat.
As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the number of elements of string 1 from the user. Then will take the number of elements of string 2 from the user. Then will take the elements from the user for string 1 and as well as for string 2. And concatenate them using the Without Using strcat .
With the help of this program, we can Concatenate Two Strings Without Using strcat
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Adding the given sting to third string. 4. Passing those variables to while loop. 5. Using the those while loop one by one to add the strings. 6. Printing the result words. |
Program to Concatenate Two Strings Using Without Using strcat :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <stdio.h> int main() { // C Program to concatenate // two strings without using strcat // Getting the two Strings to be concatenated char string1[100] = "Best tutorial site", string2[100] = " W3adda "; // Declare a new Strings // to store the concatenated String char string3[100]; int i = 0, j = 0; //Printing string one and two printf("\nFirst string is : %s", string1); printf("\nSecond string is : %s", string2); // Insert the first string in the new string while (string1[i] != '\0') { string3[j] = string1[i]; i++; j++; } // Insert the second string in the new string i = 0; while (string2[i] != '\0') { string3[j] = string2[i]; i++; j++; } string3[j] = '\0'; // Print the concatenated string printf("\nConcatenated string: %s", string3); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- string1[100] = it will hold the string value.
- string2[100] = it will hold the string value.
- i = it will hold the integer value.
- j = it will hold the integer value.
- string3 = it will hold the string value.
Input strings.
While loop body for the first string to another.
While loop body for the second string to another.
Printing Output.