Home Forums C Programming help combining strings Re: Re: help combining strings

#3472
GWILouisaxwzkla
Participant

Could try:


#include
#include
#define MAX 101
char * concat ( char s1[], char s2[], int maxlength );

int main()
{
#define MAX 101
char s1 [ MAX ];
char s2 [ MAX ];

printf("Enter the first string ");
gets(s1);
printf("Enter the second string ");
gets(s2);
printf("The combined string is: %s n", concat ( s1 , s2 , MAX ) );


return 0;
}

char * concat ( char s1[], char s2[], int maxlength )
{

if ( strlen ( s1 ) + strlen ( s2 ) > maxlength )
return s1;//stings too long , return first string as default
strcat ( s1 , s2 ); //stick s2 to the back of s1
return s1;
}