Encoders
Hub for Computer Whizzes Register to join us

Encoders > More... > C > Program

/* Function strncat(s,t,n) to concatenate at most n characters of t to the
   end of s and return the concatenated string, s must be big enough.
   (using pointers) */

#include <stdio.h>

char *strncat(char *s, char *t, int n);

main()
{
        char a[100]="hello world.\n", b[]="hello world.";
        printf("%s\n",strncat(a,b,10));
}

char *strncat(char *s, char *t, int n)
{
	char *start=s;
	while(*s)
		s++;
        while(n-- && (*s++ = *t++))
		;
	*s='\0';
	return start;
}