

string functions             Overview            string functions




The character  string is a  common formation in  C programs.  The
runtime representation  of a string is an  array of ASCII charac-
ters  that is  terminated by a  null character  (`\0').  COHERENT
uses this  representation when a  program contains a  string con-
stant; for example:


        "I am a string constant"


The address of  the first character in the string  is used as the
starting point  of the string.  A pointer to  a string holds only
this address.  Note, too, that an array of 20 characters can hold
a string  of 19 (_n_o_t 20) non-null  characters; the 20th character
is the null character that terminates the string.

The following routines are available to help manipulate strings:

iinnddeexx()   Search string for a character; use ssttrrcchhrr instead
mmeemmcchhrr()  Search buffer for a character
mmeemmccmmpp()  Compare two buffers
mmeemmccppyy()  Copy one buffer into another
mmeemmsseett()  Initialize a buffer
ppnnmmaattcchh() Match a string pattern
rriinnddeexx()  Search string for a character; use ssttrrrrcchhrr instead
ssttrrccaatt()  Concatenate two strings
ssttrrcchhrr()  Find a character in a string
ssttrrccmmpp()  Compare two string
ssttrrccppyy()  Copy one string into another
ssttrrccssppnn() Return length for which strings do not match
ssttrreerrrroorr()Translate error number into string
ssttrrlleenn()  Measure a string
ssttrrnnccaatt() Concatenate two strings
ssttrrnnccmmpp() Compare two strings
ssttrrnnccppyy() Copy one string into another
ssttrrppbbrrkk() Find first occurrence of any character in string
ssttrrrrcchhrr() Find rightmost occurrence of character
ssttrrssppnn()  Return length for which strings match
ssttrrssttrr()  Find one string within another
ssttrrttookk()  Break a string into tokens

***** Example *****

This example  reads from stdin up to NNAMES  names, each of which
is no  more than MAXLEN characters long.   It then removes dupli-
cate names,  sorts the names,  and writes the sorted  list to the
standard  output.   It   demonstrates  the  functions  shellsort,
strcat, strcmp, strcpy, and strlen.








COHERENT Lexicon                                           Page 1




string functions             Overview            string functions



#include <stdio.h>



#define NNAMES 512
#define MAXLEN 60



char *array[NNAMES];
char first[MAXLEN], mid[MAXLEN], last[MAXLEN];
char *space = " ";



int compare();
extern char *strcat();



main()
{
          register int index, count, inflag;
          register char *name;



          count = 0;
          while (scanf("%s %s %s\n", first, mid, last) == 3) {
          strcat(first, space);
          strcat(mid, space);
          name = strcat(first, (strcat(mid, last)));
          inflag = 0;



          for (index=0; index < count; index++)
           if (strcmp(array[index], name) == 0)
            inflag = 1;



          if (inflag == 0) {
           if ((array[count] =
            malloc(strlen(name) + 1)) == NULL) {
            fprintf(stderr, "Insufficient memory\n");
            exit(1);
           }
           strcpy(array[count], name);
           count++;
          }
          }





COHERENT Lexicon                                           Page 2




string functions             Overview            string functions




          shellsort(array, count, sizeof(char *), compare);
          for (index=0; index < count; index++)
          printf("%s\n", array[index]);
          exit(0);
}



compare(s1, s2)
register char **s1, **s2;
{
          extern int strcmp();
          return(strcmp(*s1, *s2));
}


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

ASCII, libraries

***** Notes *****

The ANSI standard allows adjacent string literals, e.g.:


          "hello" "world"


Adjacent string  literals are automatically  concatenated.  Thus,
the  compiler will  automatically concatenate  the  above example
into:


          "helloworld"


Because this  departs from the Kernighan  and Ritchie description
of C,  it will  generate a  warning message if  you use  the com-
piler's -VVSSBBOOOOKK option.

















COHERENT Lexicon                                           Page 3


