EmbLogic's Blog

c program to concatinate two string using function,pointer and memory allocation technique

#include
#include
#include
char *con(char *s1,char *s2);
char *input();
int main()
{
char *s1,*s2;
int i=0;
printf(“Enter string 1:”);
s1=input();
printf(“Enter string 2:”);
s2=input();
// strcat(s1,s2);
s1=con(s1,s2);
printf(“Concatination is: %s\n”,s1);
}
char *input()
{
char *s,ch;
int i=0;
s=(char *)malloc(sizeof(char));
while(1)
{
scanf(“%c”,&ch);
// ch=(char *)malloc(10*sizeof(char));

s=realloc(s,(i+2)*sizeof(char));
if(ch==10)
break;
s[i]=ch;
i++;
}
return s;
}
char *con(char *s1,char *s2)
{
int i=0,j=0;
for(i=0;i<20;i++)
{
if(*(s1+i)=='')
{
*(s1+i)=*(s2+j);
j++;

}

}

return s1;

}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>