Copy string by length, like strncpy - C String

C examples for String:String Function

Description

Copy string by length, like strncpy

Demo Code

#include <stdio.h>

void _strncpy(char *s, char *t, int n);
int getchars(char *s, int max);

int main(void)
{
    char *t = "123123testring1234";
    char *s;/*  w  w  w  .  j  a v a 2 s.  c  o  m*/

    _strncpy(s, t, 8);
    printf("strncpy: %s\n", s);

    return 0;
}

void _strncpy(char *s, char *t, int n)
{
    while (n-- > 0 && (*s++ = *t++))
        ;
    *s = '\0';
}

Related Tutorials