Encoders
Hub for Computer Whizzes Register to join us

Encoders > More... > C > Program

/* Function strncpy(s,t,n) to copy at most n characters of string t into s
   and return the copied string. It pads with '\0' if t has fewer than n
   characters. s might not be null-terminated if the length of t is n or
   more. (using pointers) */

#include <stdio.h>

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

main()
{
	char a[100];
	int i;
	clrscr();
        printf("%s\n",strncpy(a,"hello world",6));
        for(i=0;i<15;i++)
                printf("%d %c %d\n",i,a[i],a[i]);

}

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