String append: Difference between revisions

Content added Content deleted
No edit summary
Line 60: Line 60:
int main()
int main()
{
{
char str[100]="Good Morning";
char str[24]="Good Morning";
char *cstr=" to all";
char *cstr=" to all";
char *cstr2=" !!!";
char *cstr2=" !!!";
int x=0;
int x=0;
//failure when space allocated to str is insufficient.
//failure when space allocated to str is insufficient.
/* 1st method*/
strcat(str,cstr);


if(sizeof(str)>strlen(str)+strlen(cstr)+strlen(cstr2))
/*2nd method*/
x=strlen(str);
{
/* 1st method*/
sprintf(&str[x],"%s",cstr2);
strcat(str,cstr);


/*2nd method*/
printf("%s\n",str);
x=strlen(str);
sprintf(&str[x],"%s",cstr2);

printf("%s\n",str);

}
return 0;
return 0;
}</lang>
}</lang>