This program concatenates strings, for example if the first string is "c " and second string is "program" then on concatenating these two strings we get the string "c program". To concatenate two strings we use strcat function of string.h, to concatenate without using library function see another code below which uses pointers.
C programming code
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char a[100], b[100];
clrscr();
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
getch();
}
String concatenation without strcat
#include <stdio.h>
#include<conio.h>
void concatenate_string(char*, char*);
void main()
{
char original[100], add[100];
clrscr();
printf("Enter source string\n");
gets(original);
printf("Enter string to concatenate\n");
gets(add);
concatenate_string(original, add);
printf("String after concatenation is \"%s\"\n", original);
getch();
}
void concatenate_string(char *original, char *add)
{
while(*original)
original++;
while(*add)
{
*original = *add;
add++;
original++;
}
*original = '\0';
}
No comments:
Post a Comment