

strncpy()                String Function                strncpy()




Copy one string into another

#include <string.h>
cchhaarr *ssttrrnnccppyy(_s_t_r_i_n_g_1, _s_t_r_i_n_g_2, _n)
cchhaarr *_s_t_r_i_n_g_1, *_s_t_r_i_n_g_2; uunnssiiggnneedd _n;

strncpy copies up to n bytes of string2 into string1, and returns
string1.  Copying  ends when n  bytes have been copied  or a null
character  has  been  encountered,  whichever  comes  first.   If
string2 is less than n characters in length, string2 is padded to
length n with one or more null bytes.

***** Example *****

This example,  called sswwaapp.cc, reads a file  of names, and changes
them from the format


        first_name  [middle_initial] last_name


to the format


        last_name, first_name [middle_initial]


It demonstrates ssttrrnnccppyy, ssttrrnnccaatt, ssttrrnnccmmpp, and iinnddeexx.


#include <stdio.h>
#define NNAMES 512
#define MAXLEN 60



char *array[NNAMES];
char gname[MAXLEN], lname[MAXLEN];
extern int strncmp(), strcomp();
extern char *strcpy(), *strncpy(), *strncat(), *index();



main(argc, argv)
int argc; char *argv[];
{
        FILE *fp;
        register int count, num;
        register char  *name, string[60], *cptr, *eptr;
        unsigned glength, length;






COHERENT Lexicon                                           Page 1




strncpy()                String Function                strncpy()



        if (--argc != 1) {
                fprintf (stderr, "Usage: swap filename\n");
                exit(1);
        }



        if ((fp = fopen(argv[1], "r")) == NULL)
                printf("Cannot open %s\n", argv[1]);
        count = 0;



        while (fgets(string, 60, fp) != NULL) {
                if ((cptr = index(string, '.')) != NULL) {
                        cptr++;
                        cptr++;
                } else if ((cptr = index(string,' ')) != NULL)
                        cptr++;



                strcpy(lname, cptr);
                eptr = index(lname, '\n');
                *eptr = ',';



                strcat(lname," ");
                glength = (unsigned)(strlen(string) - strlen(cptr));
                strncpy(gname, string, glength);



                name = strncat(lname, gname, glength);
                length = (unsigned)strlen(name);
                array[count] = malloc(length + 1);



                strcpy(array[count],name);
                count++;
        }



        for (num = 0; num < count; num++)
                printf("%s\n", array[num]);
        exit(0);
}


***** See Also *****

strcpy(), string functions, string.h


COHERENT Lexicon                                           Page 2




strncpy()                String Function                strncpy()




***** Notes *****

string1 must point to enough  space to n bytes; otherwise, a por-
tion of the program or operating system may be overwritten.




















































COHERENT Lexicon                                           Page 3


