|
|
1.1 root 1:
2:
3: string functions Overview string functions
4:
5:
6:
7:
8: The character string is a common formation in C programs. The
9: runtime representation of a string is an array of ASCII charac-
10: ters that is terminated by a null character (`\0'). COHERENT
11: uses this representation when a program contains a string con-
12: stant; for example:
13:
14:
15: "I am a string constant"
16:
17:
18: The address of the first character in the string is used as the
19: starting point of the string. A pointer to a string holds only
20: this address. Note, too, that an array of 20 characters can hold
21: a string of 19 (_n_o_t 20) non-null characters; the 20th character
22: is the null character that terminates the string.
23:
24: The following routines are available to help manipulate strings:
25:
26: iinnddeexx() Search string for a character; use ssttrrcchhrr instead
27: mmeemmcchhrr() Search buffer for a character
28: mmeemmccmmpp() Compare two buffers
29: mmeemmccppyy() Copy one buffer into another
30: mmeemmsseett() Initialize a buffer
31: ppnnmmaattcchh() Match a string pattern
32: rriinnddeexx() Search string for a character; use ssttrrrrcchhrr instead
33: ssttrrccaatt() Concatenate two strings
34: ssttrrcchhrr() Find a character in a string
35: ssttrrccmmpp() Compare two string
36: ssttrrccppyy() Copy one string into another
37: ssttrrccssppnn() Return length for which strings do not match
38: ssttrreerrrroorr()Translate error number into string
39: ssttrrlleenn() Measure a string
40: ssttrrnnccaatt() Concatenate two strings
41: ssttrrnnccmmpp() Compare two strings
42: ssttrrnnccppyy() Copy one string into another
43: ssttrrppbbrrkk() Find first occurrence of any character in string
44: ssttrrrrcchhrr() Find rightmost occurrence of character
45: ssttrrssppnn() Return length for which strings match
46: ssttrrssttrr() Find one string within another
47: ssttrrttookk() Break a string into tokens
48:
49: ***** Example *****
50:
51: This example reads from stdin up to NNAMES names, each of which
52: is no more than MAXLEN characters long. It then removes dupli-
53: cate names, sorts the names, and writes the sorted list to the
54: standard output. It demonstrates the functions shellsort,
55: strcat, strcmp, strcpy, and strlen.
56:
57:
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: string functions Overview string functions
70:
71:
72:
73: #include <stdio.h>
74:
75:
76:
77: #define NNAMES 512
78: #define MAXLEN 60
79:
80:
81:
82: char *array[NNAMES];
83: char first[MAXLEN], mid[MAXLEN], last[MAXLEN];
84: char *space = " ";
85:
86:
87:
88: int compare();
89: extern char *strcat();
90:
91:
92:
93: main()
94: {
95: register int index, count, inflag;
96: register char *name;
97:
98:
99:
100: count = 0;
101: while (scanf("%s %s %s\n", first, mid, last) == 3) {
102: strcat(first, space);
103: strcat(mid, space);
104: name = strcat(first, (strcat(mid, last)));
105: inflag = 0;
106:
107:
108:
109: for (index=0; index < count; index++)
110: if (strcmp(array[index], name) == 0)
111: inflag = 1;
112:
113:
114:
115: if (inflag == 0) {
116: if ((array[count] =
117: malloc(strlen(name) + 1)) == NULL) {
118: fprintf(stderr, "Insufficient memory\n");
119: exit(1);
120: }
121: strcpy(array[count], name);
122: count++;
123: }
124: }
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
133:
134:
135: string functions Overview string functions
136:
137:
138:
139:
140: shellsort(array, count, sizeof(char *), compare);
141: for (index=0; index < count; index++)
142: printf("%s\n", array[index]);
143: exit(0);
144: }
145:
146:
147:
148: compare(s1, s2)
149: register char **s1, **s2;
150: {
151: extern int strcmp();
152: return(strcmp(*s1, *s2));
153: }
154:
155:
156: ***** See Also *****
157:
158: ASCII, libraries
159:
160: ***** Notes *****
161:
162: The ANSI standard allows adjacent string literals, e.g.:
163:
164:
165: "hello" "world"
166:
167:
168: Adjacent string literals are automatically concatenated. Thus,
169: the compiler will automatically concatenate the above example
170: into:
171:
172:
173: "helloworld"
174:
175:
176: Because this departs from the Kernighan and Ritchie description
177: of C, it will generate a warning message if you use the com-
178: piler's -VVSSBBOOOOKK option.
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196: COHERENT Lexicon Page 3
197:
198:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.