HP 3000 Manuals

strncmp [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation


HP C/iX Library Reference Manual

strncmp 

Compares two strings up to a maximum of n characters and returns an
integer result of the comparison.

Syntax 

     #include <string.h>
     int strncmp(const char *s1, const char *s2, size_t n);

Parameters 

s1, s2        Pointers to character strings.

n             Specifies the maximum number of characters to compare in
              both s1 and s2.

Return Values 

<0            s1 is less than s2.

=0            s1 and s2 are equal.

>0            s1 is greater than s2.

Description 

In the strncmp function, s1 and s2 are character pointers to the
null-terminated character strings to be compared.  strncmp() compares
at most n characters of both strings.  Neither string need be
null-terminated if n is less than or equal to the length of the shorter
string.

Example 

The following program fragment uses strncmp() to analyze the contents of
a file coded with macros.  In this example, a period (.)  at the
beginning of a line indicates a macro.  The program reads each line of
the file and keeps a count of the number of times selected macros are
used, and prints a summary of its findings at the end.

        #include <stdio.h>
        main(argc, argv)
        int argc;
        char *argv[ ];
        {
           char *fgets(), line[100];
           FILE *fp;
           int nsh, npp, ntp, nrs, nre, npd, nip, nmisc, nlines;

           nsh = npp = ntp = nrs = nre = npd = nip = nmisc = nlines = 0;

           if(argc != 2) {
                fprintf(stderr, "Usage: count file\n");
                exit(2);
           }

           fp = fopen(argv[1], "r");
           if(fp == NULL) {
                fprintf(stderr, "Can't open %s.\n", argv[1]);
                exit(1);
           }

           while(fgets(line, 100, fp) != NULL) {
                if(strncmp(line, ".SH", 3) == 0)
                     nsh++;
                else if(strncmp(line, ".PP", 3) == 0)
                     npp++;
                else if(strncmp(line, ".TP", 3) == 0)
                     ntp++;
                else if(strncmp(line, ".RS", 3) == 0)
                     nrs++;
                else if(strncmp(line, ".RE", 3) == 0)
                     nre++;
                else if(strncmp(line, ".PD", 3) == 0)
                     npd++;
                else if(strncmp(line, ".IP", 3) == 0)
                     nip++;
                else if(line[0] == '.')
                     nmisc++;
                nlines++;
           }

           printf("No. of lines: %d\n\n", nlines);
           printf("No. of .SH's: %d\n", nsh);
           printf("No. of .PP's: %d\n", npp);
           printf("No. of .TP's: %d\n", ntp);
           printf("No. of .RS's: %d\n", nrs);
           printf("No. of .RE's: %d\n", nre);
           printf("No. of .PD's: %d\n", npd);
           printf("No. of .IP's: %d\n", nip);
           printf("No. of misc. macros: %d\n", nmisc);

           fclose(fp);
           exit(0);
        }

In the program above, strncmp() compares the first three characters of
each line read.  If the first three characters match a particular macro,
the appropriate counter is incremented.  If the line begins with ".", but
is not one of the macros being searched for, the "miscellaneous" counter
is incremented.  The total number of lines in the file is also given.

See Also 

strcmp(), strcoll(), memcmp(), ANSI C 4.11.4.4, POSIX.1 8.1



MPE/iX 5.0 Documentation