Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ () — Coherent 3.1.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought


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 (not 20) non-null  characters; the 20th character
is the null character that terminates the string.

The following routines are available to help manipulate strings:

index()   Search string for a character; use strchr instead
memchr()  Search buffer for a character
memcmp()  Compare two buffers
memcpy()  Copy one buffer into another
memset()  Initialize a buffer
pnmatch() Match a string pattern
rindex()  Search string for a character; use strrchr instead
strcat()  Concatenate two strings
strchr()  Find a character in a string
strcmp()  Compare two string
strcpy()  Copy one string into another
strcspn() Return length for which strings do not match
strerror()Translate error number into string
strlen()  Measure a string
strncat() Concatenate two strings
strncmp() Compare two strings
strncpy() Copy one string into another
strpbrk() Find first occurrence of any character in string
strrchr() Find rightmost occurrence of character
strspn()  Return length for which strings match
strstr()  Find one string within another
strtok()  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 -VSBOOK option.

















COHERENT Lexicon                                           Page 3


Typewritten Software • bear@typewritten.org • Edmonds, WA 98026