Write a program to concatenate two strings using function (without using library function).
Input
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
void stringCat (char *s1,char *s2);
int main()
{
char str1[MAX_SIZE],str2[MAX_SIZE];
printf("Enter string 1 : ");
scanf("%[^\n]s",str1);
getchar();
printf("\nEnter string 2 : ");
scanf("%[^\n]s",str2);
stringCat(str1,str2);
printf("\nConcatenated String is : %s ",str1);
printf("\n");
return 0;
}
void stringCat (char *s1,char *s2)
{
int len,i;
len=strlen(s1)+strlen(s2);
if(len>MAX_SIZE)
{
printf("\nCan not Concatenate !!!");
return;
}
len=strlen(s1);
for(i=0;i< strlen(s2); i++)
{
s1[len+i]=s2[i];
}
s1[len+i]='\0';
}
Output
Enter string 1 : Dipshikha
Enter string 2 : Roy
Concatenated String is : DipshikhaRoy
Input
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
void stringCat (char *s1,char *s2);
int main()
{
char str1[MAX_SIZE],str2[MAX_SIZE];
printf("Enter string 1 : ");
scanf("%[^\n]s",str1);
getchar();
printf("\nEnter string 2 : ");
scanf("%[^\n]s",str2);
stringCat(str1,str2);
printf("\nConcatenated String is : %s ",str1);
printf("\n");
return 0;
}
void stringCat (char *s1,char *s2)
{
int len,i;
len=strlen(s1)+strlen(s2);
if(len>MAX_SIZE)
{
printf("\nCan not Concatenate !!!");
return;
}
len=strlen(s1);
for(i=0;i< strlen(s2); i++)
{
s1[len+i]=s2[i];
}
s1[len+i]='\0';
}
Output
Enter string 1 : Dipshikha
Enter string 2 : Roy
Concatenated String is : DipshikhaRoy
No comments:
Post a Comment