Using the strncpy() function to copy sub string - C String

C examples for String:String Function

Description

Using the strncpy() function to copy sub string

Demo Code

#include <stdio.h>
#include <string.h>

char dest[] = "..........................";
char source[] = "abcdefghijklmnopqrstuvwxyz";

int main( void )
{
    size_t n;/*from   w ww  . j a  v  a  2s .  c o  m*/

    while (1)
    {
        puts("Enter the number of characters to copy (1-26)");
        scanf("%d", &n);

        if (n > 0 && n< 27)
            break;
    }

    printf("\nBefore strncpy destination = %s", dest);

    strncpy(dest, source, n);

    printf("\nAfter strncpy destination = %s\n", dest);
    return 0;
}

Result


Related Tutorials