C program to concatenate two string with or without using strcat

C program to concatenate two string with or without using strcat function
         C programming provide string handling function strcat() which concatenate two given string. It means strcat function append one string with another.

General syntax of strcat function
                                                       strcat(string_1, string_2);

In which string_1 and string_2 are character arrays. And when strcat function is execute in c program then it append string_2 with string_1.

Now lets see that how strcat function of string handling function in c programming will works.
In which it removing null character at the end of string_1 and placing string_2 from there.

For Example we have two string or character arrays str1[12] and str2[8].

str1[12]="Hello";
str2[10]="world";

So it will represented in character array as follows:

str1:
123456789101112
Hello\n

str2:
12345678910
world\n

Now when we execute statement strcat(str1,str2); then it append or concatenate the str2 with str1 and it store in str1 as follows:
str1 after executing statement strcat(str1,str2)
123456789101112
Helloworld\n

Remember one thing that you have to sure that the size of first string is large enough to store both the string because in strcat function after the execution of function str2 will append with str1.

C programming example to concatenate two string by using strcat function

#include<stdio.h>
#include<string.h>

void main()
{

char str1[50],str2[50];
clrscr();
printf("Enter First string: ");
scanf("%s",&str1);

printf("Enter second string: ");
scanf("%s",&str2);
strcat(str1,str2);

printf("String concated in first string  " );
printf("\nFirst string is : %s",str1);

getch();

}

c program to concatenate two string by using strcat function

Concatenate two string without using string handling function strcat.
Now lets try to concatenate two string without using string handling function strcat. In which we do same thing which we perform in above, str2 append with str1. For that first we will find the length of str1. So we can start appending str2 with str1 from its end. Now then after we will start over loop till we not found the null value of str2. And every time str2[i] will copy in str1[len+i] and the value of i will increment until str2[i]='\0' condition found.
For more and better understanding of this c program logic check the c program example.


C programming example to concatenate two string without using strcat function. 

#include<stdio.h>

void main()
{

int i=0,len=0,len1=0;
char str1[50],str2[50];

clrscr();

printf("Enter First string : ");
scanf("%s",&str1);

printf("Enter Second string : ");
scanf("%s",&str2);

while(str1[len]!='\0')
{
len++;
}

while(str2[len1]!='\0')
{
str1[len]=str2[len1];
len1++;
len++;
}
str1[len]='\0';
printf("\n\n\str1 is :%s",str1);

getch();

}

C program to concatenate two string without using strcat function.</b> </div>
<br />
<div class=





0 comments:

Post a Comment