C语言字符串函数大全(转自百度百科)

  1 函数名: strcat 
  2 功  能: 字符串拼接函数 
  3 用  法: char *strcat(char *destin, char *source); 
  4 程序例:
  5 #include <string.h> 
  6 #include <stdio.h>
  7 int main(void) 
  8 { 
  9    char destination[25]; 
 10    char *blank = " ", *c = "C++", *Borland = "Borland";
 11    strcpy(destination, Borland); 
 12    strcat(destination, blank); 
 13    strcat(destination, c);
 14    printf("%s\n", destination); 
 15    return 0; 
 16 } 
 17   
 18   
 19  
 20 函数名: strchr 
 21 功  能: 在一个串中查找给定字符的第一个匹配之处\ 
 22 用  法: char *strchr(char *str, char c); 
 23 程序例:
 24 #include <string.h> 
 25 #include <stdio.h>
 26 int main(void) 
 27  { 
 28     char string[15]; 
 29     char *ptr, c = 'r';
 30     strcpy(string, "This is a string"); 
 31     ptr = strchr(string, c); 
 32     if (ptr) 
 33        printf("The character %c is at position: %d\n", c, ptr-string); 
 34     else 
 35        printf("The character was not found\n"); 
 36     return 0; 
 37  } 
 38   
 39   
 40  
 41 函数名: strcmp 
 42 功  能: 串比较 
 43 用  法: int strcmp(char *str1, char *str2); 
 44 看Asic码,str1>str2,返回值 > 0;两串相等,返回0
 45 程序例:
 46 #include <string.h> 
 47 #include <stdio.h>
 48 int main(void) 
 49  { 
 50     char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; 
 51     int ptr;
 52     ptr = strcmp(buf2, buf1); 
 53     if (ptr > 0) 
 54        printf("buffer 2 is greater than buffer 1\n"); 
 55     else 
 56        printf("buffer 2 is less than buffer 1\n");
 57     ptr = strcmp(buf2, buf3); 
 58     if (ptr > 0) 
 59        printf("buffer 2 is greater than buffer 3\n"); 
 60     else 
 61        printf("buffer 2 is less than buffer 3\n");
 62     return 0; 
 63  } 
 64   
 65   
 66  
 67 函数名: strncmpi 
 68 功  能: 将一个串中的一部分与另一个串比较, 不管大小写 
 69 用  法: int strncmpi(char *str1, char *str2, unsigned maxlen); 
 70 程序例:
 71 #include <string.h> 
 72 #include <stdio.h>
 73 int main(void) 
 74 { 
 75    char *buf1 = "BBB", *buf2 = "bbb"; 
 76    int ptr;
 77    ptr = strcmpi(buf2, buf1);
 78    if (ptr > 0) 
 79       printf("buffer 2 is greater than buffer 1\n");
 80    if (ptr < 0) 
 81       printf("buffer 2 is less than buffer 1\n");
 82    if (ptr == 0) 
 83       printf("buffer 2 equals buffer 1\n");
 84    return 0; 
 85 } 
 86   
 87   
 88  
 89 函数名: strcpy 
 90 功  能: 串拷贝 
 91 用  法: char *strcpy(char *str1, char *str2); 
 92 程序例:
 93 #include <stdio.h> 
 94 #include <string.h>
 95 int main(void) 
 96  { 
 97     char string[10]; 
 98     char *str1 = "abcdefghi";
 99     strcpy(string, str1); 
100     printf("%s\n", string); 
101     return 0; 
102  } 
103   
104   
105  
106 函数名: strcspn 
107 功  能: 在串中查找第一个给定字符集内容的段 
108 用  法: int strcspn(char *str1, char *str2); 
109 程序例:
110 #include <stdio.h> 
111 #include <string.h> 
112 #include <alloc.h>
113 int main(void) 
114  { 
115     char *string1 = "1234567890"; 
116     char *string2 = "747DC8"; 
117     int length;
118     length = strcspn(string1, string2); 
119     printf("Character where strings intersect is at position %d\n", length);
120     return 0; 
121  } 
122   
123   
124  
125 函数名: strdup 
126 功  能: 将串拷贝到新建的位置处 
127 用  法: char *strdup(char *str); 
128 程序例:
129 #include <stdio.h> 
130 #include <string.h> 
131 #include <alloc.h>
132 int main(void) 
133  { 
134     char *dup_str, *string = "abcde";
135     dup_str = strdup(string); 
136     printf("%s\n", dup_str); 
137     free(dup_str);
138     return 0; 
139  } 
140   
141   
142  
143 函数名: stricmp 
144 功  能: 以大小写不敏感方式比较两个串 
145 用  法: int stricmp(char *str1, char *str2); 
146 程序例:
147 #include <string.h> 
148 #include <stdio.h>
149 int main(void) 
150 { 
151    char *buf1 = "BBB", *buf2 = "bbb"; 
152    int ptr;
153    ptr = stricmp(buf2, buf1);
154    if (ptr > 0) 
155       printf("buffer 2 is greater than buffer 1\n");
156    if (ptr < 0) 
157       printf("buffer 2 is less than buffer 1\n");
158    if (ptr == 0) 
159       printf("buffer 2 equals buffer 1\n");
160    return 0; 
161 } 
162   
163  
164 函数名: strerror 
165 功  能: 返回指向错误信息字符串的指针 
166 用  法: char *strerror(int errnum); 
167 程序例:
168 #include <stdio.h> 
169 #include <errno.h>
170 int main(void) 
171 { 
172    char *buffer; 
173    buffer = strerror(errno); 
174    printf("Error: %s\n", buffer); 
175    return 0; 
176 } 
177   
178   
179  
180 函数名: strcmpi 
181 功  能: 将一个串与另一个比较, 不管大小写 
182 用  法: int strcmpi(char *str1, char *str2); 
183 程序例:
184 #include <string.h> 
185 #include <stdio.h>
186 int main(void) 
187 { 
188    char *buf1 = "BBB", *buf2 = "bbb"; 
189    int ptr;
190    ptr = strcmpi(buf2, buf1);
191    if (ptr > 0) 
192       printf("buffer 2 is greater than buffer 1\n");
193    if (ptr < 0) 
194       printf("buffer 2 is less than buffer 1\n");
195    if (ptr == 0) 
196       printf("buffer 2 equals buffer 1\n");
197    return 0; 
198 } 
199   
200   
201  
202 函数名: strncmp 
203 功  能: 串比较 
204 用  法: int strncmp(char *str1, char *str2, int maxlen); 
205 程序例:
206 #include <string.h> 
207 #include <stdio.h>
208 int  main(void)
209 { 
210    char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; 
211    int ptr;
212    ptr = strncmp(buf2,buf1,3); 
213    if (ptr > 0) 
214       printf("buffer 2 is greater than buffer 1\n"); 
215    else 
216       printf("buffer 2 is less than buffer 1\n");
217    ptr = strncmp(buf2,buf3,3); 
218    if (ptr > 0) 
219       printf("buffer 2 is greater than buffer 3\n"); 
220    else 
221       printf("buffer 2 is less than buffer 3\n");
222    return(0); 
223 } 
224   
225  
226 函数名: strncmpi 
227 功  能: 把串中的一部分与另一串中的一部分比较, 不管大小写 
228 用  法: int strncmpi(char *str1, char *str2); 
229 程序例:
230 #include <string.h> 
231 #include <stdio.h>
232 int main(void) 
233 { 
234    char *buf1 = "BBBccc", *buf2 = "bbbccc"; 
235    int ptr;
236    ptr = strncmpi(buf2,buf1,3);
237    if (ptr > 0) 
238       printf("buffer 2 is greater than buffer 1\n");
239    if (ptr < 0) 
240       printf("buffer 2 is less than buffer 1\n");
241    if (ptr == 0) 
242       printf("buffer 2 equals buffer 1\n");
243    return 0; 
244 } 
245   
246  
247 函数名: strncpy 
248 功  能: 串拷贝 
249 用  法: char *strncpy(char *destin, char *source, int maxlen); 
250 程序例:
251 #include <stdio.h> 
252 #include <string.h>
253 int main(void) 
254 { 
255    char string[10]; 
256    char *str1 = "abcdefghi";
257    strncpy(string, str1, 3); 
258    string[3] = '\0'; 
259    printf("%s\n", string); 
260    return 0; 
261 } 
262   
263  
264 函数名: strnicmp 
265 功  能: 不注重大小写地比较两个串 
266 用  法: int strnicmp(char *str1, char *str2, unsigned maxlen); 
267 程序例:
268 #include <string.h> 
269 #include <stdio.h>
270 int main(void) 
271 { 
272    char *buf1 = "BBBccc", *buf2 = "bbbccc"; 
273    int ptr;
274    ptr = strnicmp(buf2, buf1, 3);
275    if (ptr > 0) 
276       printf("buffer 2 is greater than buffer 1\n");
277    if (ptr < 0) 
278       printf("buffer 2 is less than buffer 1\n");
279    if (ptr == 0) 
280       printf("buffer 2 equals buffer 1\n");
281    return 0; 
282 } 
283   
284   
285  
286 函数名: strnset 
287 功  能: 将一个串中的所有字符都设为指定字符 
288 用  法: char *strnset(char *str, char ch, unsigned n); 
289 程序例:
290 #include <stdio.h> 
291 #include <string.h>
292 int main(void) 
293 { 
294    char *string = "abcdefghijklmnopqrstuvwxyz"; 
295    char letter = 'x';
296    printf("string before strnset: %s\n", string); 
297    strnset(string, letter, 13); 
298    printf("string after  strnset: %s\n", string);
299    return 0; 
300 } 
301   
302  
303 函数名: strpbrk 
304 功  能: 在串中查找给定字符集中的字符 
305 用  法: char *strpbrk(char *str1, char *str2); 
306 程序例:
307 #include <stdio.h> 
308 #include <string.h>
309 int main(void) 
310 { 
311    char *string1 = "abcdefghijklmnopqrstuvwxyz"; 
312    char *string2 = "onm"; 
313    char *ptr;
314    ptr = strpbrk(string1, string2);
315    if (ptr) 
316       printf("strpbrk found first character: %c\n", *ptr); 
317    else 
318       printf("strpbrk didn't find character in set\n");
319    return 0; 
320 } 
321   
322   
323  
324 函数名: strrchr 
325 功  能: 在串中查找指定字符的最后一个出现 
326 用  法: char *strrchr(char *str, char c); 
327 程序例:
328 #include <string.h> 
329 #include <stdio.h>
330 int main(void) 
331 { 
332    char string[15]; 
333    char *ptr, c = 'r';
334    strcpy(string, "This is a string"); 
335    ptr = strrchr(string, c); 
336    if (ptr) 
337       printf("The character %c is at position: %d\n", c, ptr-string); 
338    else 
339       printf("The character was not found\n"); 
340    return 0; 
341 } 
342   
343   
344  
345 函数名: strrev 
346 功  能: 串倒转 
347 用  法: char *strrev(char *str); 
348 程序例:
349 #include <string.h> 
350 #include <stdio.h>
351 int main(void) 
352 { 
353    char *forward = "string";
354    printf("Before strrev(): %s\n", forward); 
355    strrev(forward); 
356    printf("After strrev():  %s\n", forward); 
357    return 0; 
358 } 
359  
360 函数名: strset 
361 功  能: 将一个串中的所有字符都设为指定字符 
362 用  法: char *strset(char *str, char c); 
363 程序例:
364 #include <stdio.h> 
365 #include <string.h>
366 int main(void) 
367 { 
368    char string[10] = "123456789"; 
369    char symbol = 'c';
370    printf("Before strset(): %s\n", string); 
371    strset(string, symbol); 
372    printf("After strset():  %s\n", string); 
373    return 0; 
374 } 
375   
376   
377  
378 函数名: strspn 
379 功  能: 在串中查找指定字符集的子集的第一次出现 
380 用  法: int strspn(char *str1, char *str2); 
381 程序例:
382 #include <stdio.h> 
383 #include <string.h> 
384 #include <alloc.h>
385 int main(void) 
386 { 
387    char *string1 = "1234567890"; 
388    char *string2 = "123DC8"; 
389    int length;
390    length = strspn(string1, string2); 
391    printf("Character where strings differ is at position %d\n", length); 
392    return 0; 
393 } 
394   
395  
396 函数名: strstr 
397 功  能: 在串中查找指定字符串的第一次出现 
398 用  法: char *strstr(char *str1, char *str2); 
399 程序例:
400 #include <stdio.h> 
401 #include <string.h>
402 int main(void) 
403 { 
404    char *str1 = "Borland International", *str2 = "nation", *ptr;
405    ptr = strstr(str1, str2); 
406    printf("The substring is: %s\n", ptr); 
407    return 0; 
408 } 
409   
410  
411 函数名: strtod 
412 功  能: 将字符串转换为double型值 
413 用  法: double strtod(char *str, char **endptr); 
414 程序例:
415 #include <stdio.h> 
416 #include <stdlib.h>
417 int main(void) 
418 { 
419    char input[80], *endptr; 
420    double value;
421    printf("Enter a floating point number:"); 
422    gets(input); 
423    value = strtod(input, &endptr); 
424    printf("The string is %s the number is %lf\n", input, value); 
425    return 0; 
426 } 
427   
428   
429  
430 函数名: strtok 
431 功  能: 查找由在第二个串中指定的分界符分隔开的单词 
432 用  法: char *strtok(char *str1, char *str2); 
433 程序例:
434 #include <string.h> 
435 #include <stdio.h>
436 int main(void) 
437 { 
438    char input[16] = "abc,d"; 
439    char *p;
440    /* strtok places a NULL terminator 
441    in front of the token, if found */ 
442    p = strtok(input, ","); 
443    if (p)   printf("%s\n", p);
444    /* A second call to strtok using a NULL 
445    as the first parameter returns a pointer 
446    to the character following the token  */ 
447    p = strtok(NULL, ","); 
448    if (p)   printf("%s\n", p); 
449    return 0; 
450 } 
451   
452   
453  
454 函数名: strtol 
455 功  能: 将串转换为长整数 
456 用  法: long strtol(char *str, char **endptr, int base); 
457 程序例:
458 #include <stdlib.h> 
459 #include <stdio.h>
460 int main(void) 
461 { 
462    char *string = "87654321", *endptr; 
463    long lnumber;
464    /* strtol converts string to long integer  */ 
465    lnumber = strtol(string, &endptr, 10); 
466    printf("string = %s  long = %ld\n", string, lnumber);
467    return 0; 
468 } 
469  
470 函数名: strupr 
471 功  能: 将串中的小写字母转换为大写字母 
472 用  法: char *strupr(char *str); 
473 程序例:
474 #include <stdio.h> 
475 #include <string.h>
476 int main(void) 
477 { 
478    char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
479    /* converts string to upper case characters */ 
480    ptr = strupr(string); 
481    printf("%s\n", ptr); 
482    return 0; 
483 } 
484   
485   
486  
487 函数名: swab 
488 功  能: 交换字节 
489 用  法: void swab (char *from, char *to, int nbytes); 
490 程序例:
491 #include <stdlib.h> 
492 #include <stdio.h> 
493 #include <string.h>
494 char source[15] = "rFna koBlrna d"; 
495 char target[15];
496 int main(void) 
497 { 
498    swab(source, target, strlen(source)); 
499    printf("This is target: %s\n", target); 
500    return 0; 
501 }

 

原文转自:http://www.cnblogs.com/shangyu/archive/2012/02/12/2347710.html

posted @ 2012-09-02 11:02  山路水桥  阅读(339)  评论(0编辑  收藏  举报