Like numbers you can also concatenate strings or paragraphs. There are several methods for concatenation but I am simplifying two out of all. One is with strcat function and other is by using arrays.
/*Concatenate Two Strings with StrCat Function*/
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
char a[100], b[100];
printf("\nEnter First String:\n");
gets(a);
printf("\nEnter Second String:\n");
gets(b);
strcat(a,b);
printf("\nString After Concatenation:: \n\n%s",a);
getch();
}
/*Concatenate Two Strings without StrCat*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
clrscr();
int i=0,j=0;
char str1[20],str2[20];
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before concatenation the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
str1[i++]=str2[j++];
}
str1[i]='\0';
printf("After concatenation the strings are\n");
puts(str1);
return 0;
}
#include<string.h>
#include<conio.h>
int main()
{
clrscr();
int i=0,j=0;
char str1[20],str2[20];
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before concatenation the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
str1[i++]=str2[j++];
}
str1[i]='\0';
printf("After concatenation the strings are\n");
puts(str1);
return 0;
}
No comments:
Post a Comment