|
|
coherent
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:
iinnddeexx() Search string for a character; use ssttrrcchhrr instead
mmeemmcchhrr() Search buffer for a character
mmeemmccmmpp() Compare two buffers
mmeemmccppyy() Copy one buffer into another
mmeemmsseett() Initialize a buffer
ppnnmmaattcchh() Match a string pattern
rriinnddeexx() Search string for a character; use ssttrrrrcchhrr instead
ssttrrccaatt() Concatenate two strings
ssttrrcchhrr() Find a character in a string
ssttrrccmmpp() Compare two string
ssttrrccppyy() Copy one string into another
ssttrrccssppnn() Return length for which strings do not match
ssttrreerrrroorr()Translate error number into string
ssttrrlleenn() Measure a string
ssttrrnnccaatt() Concatenate two strings
ssttrrnnccmmpp() Compare two strings
ssttrrnnccppyy() Copy one string into another
ssttrrppbbrrkk() Find first occurrence of any character in string
ssttrrrrcchhrr() Find rightmost occurrence of character
ssttrrssppnn() Return length for which strings match
ssttrrssttrr() Find one string within another
ssttrrttookk() 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 -VVSSBBOOOOKK option.
COHERENT Lexicon Page 3
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.