Home › Forums › C Programming › help combining strings › Re: Re: help combining strings
November 6, 2008 at 5:03 pm
#3472
GWILouisaxwzkla
Participant
Could try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <br /> #include <br /> #include <br /> #define MAX 101<br /> char * concat ( char s1[], char s2[], int maxlength );<br /> <br /> int main()<br /> {<br /> #define MAX 101<br /> char s1 [ MAX ];<br /> char s2 [ MAX ];<br /> <br /> printf("Enter the first string ");<br /> gets(s1);<br /> printf("Enter the second string ");<br /> gets(s2);<br /> printf("The combined string is: %s n", concat ( s1 , s2 , MAX ) );<br /> <br /> <br /> return 0;<br /> }<br /> <br /> char * concat ( char s1[], char s2[], int maxlength )<br /> {<br /> <br /> if ( strlen ( s1 ) + strlen ( s2 ) > maxlength )<br /> return s1;//stings too long , return first string as default<br /> strcat ( s1 , s2 ); //stick s2 to the back of s1<br /> return s1;<br /> } |