Monday, July 27, 2009

C program for string concatenation?

C program coding

C program for string concatenation?
Try using strcat() function (Be sure to include string.h). The syntax of this function is :





newstr = strcat(str1, str2 );





This means the function accepts two string "str1" and "str2" as arguments and concatenates them. The concatenated string is assigned to the variable "newstr".
Reply:str1="string1"


str2="string2"


str3 = str1 + str2;


this is simple. note its just a partial coding.
Reply:String a = strcat(str1, str2 );
Reply:Careful with the first parameter. There must be enough space allocated in that memory space because the second string will be appended. See the documentation link.





Are you asking for a function or a program? If a program, then I think we don't understand the question.
Reply:There is already a function, strcat(), which does this for you.
Reply:this program concats 2 strings a,b using a user-defined function conc().


a and b are passed to the function and accepted in the variables x and y . the concated word is stored in z.


void conc(char x,char y)


{


int l1=strlen(x);


int l2=strlen(y);


l3=l1+l2;


char z[l3];


for(int i=0;i%26lt;l3;i++)


{


z[i]=x[i];


z[i+l1]=y[i];


}


cout.write(z);


}


hope this helps.
Reply:If you need ready function, use strcat().


Note that for strcat(s1, s2) s1 must have enough room to contain resulting string.


If you need your own function, it will be as:


s1 = realloc( s1, strlen(s1)+strlen(s2)+1);


strcpy( s1+strlen(s1), s2 );


That's all.
Reply:already defined fn in string library.


strcat()

flower garden

No comments:

Post a Comment