C语言字符串函数集锦(二)

开发 后端
本文介绍的是C语言中的字符串函数,每个函数都介绍了用法,并举例介绍的。希望对大家有帮助,一起来看。

说起字符串函数,我想大家都不陌生。字符串函数对二进制数据、字符串和表达式执行不同的运算。下面总结了C语言中的字符串函数。

13、函数名: strncmpi

功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写

用 法:

  1. int strncmpi(char *str1, char *str2);  

 

程序例:

 

  1. #include <string.h>   
  2. #include <stdio.h>   
  3. int main(void)   
  4. {   
  5. char *buf1 = "BBBccc", *buf2 = "bbbccc";   
  6. int ptr;   
  7. ptr = strncmpi(buf2,buf1,3);   
  8. if (ptr > 0)   
  9. printf("buffer 2 is greater than buffer 1\n");   
  10. if (ptr < 0)   
  11. printf("buffer 2 is less than buffer 1\n");   
  12. if (ptr == 0)   
  13. printf("buffer 2 equals buffer 1\n");   
  14. return 0;   
  15. }  

 

 

14、函数名: strncpy

功 能: 串拷贝

用 法:

  1. char *strncpy(char *destin, char *source, int maxlen);  

 

程序例:

 

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. int main(void)   
  4. {   
  5. char string[10];   
  6. char *str1 = "abcdefghi";   
  7. strncpy(string, str1, 3);   
  8. string[3] = '\0';   
  9. printf("%s\n", string);   
  10. return 0;   
  11. }  

 

 

15、函数名: strnicmp

功 能: 不注重大小写地比较两个串

用 法:

  1. int strnicmp(char *str1, char *str2, unsigned maxlen);  

程序例:

 

  1. #include <string.h>   
  2. #include <stdio.h>   
  3. int main(void)   
  4. {   
  5. char *buf1 = "BBBccc", *buf2 = "bbbccc";   
  6. int ptr;   
  7. ptr = strnicmp(buf2, buf1, 3);   
  8. if (ptr > 0)   
  9. printf("buffer 2 is greater than buffer 1\n");   
  10. if (ptr < 0)   
  11. printf("buffer 2 is less than buffer 1\n");   
  12. if (ptr == 0)   
  13. printf("buffer 2 equals buffer 1\n");   
  14. return 0;   
  15. }  

 

 

16、函数名: strnset

功 能: 将一个串中的所有字符都设为指定字符

用 法:

  1. char *strnset(char *str, char ch, unsigned n);  

 

程序例:

 

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. int main(void)   
  4. {   
  5. char *string = "abcdefghijklmnopqrstuvwxyz";   
  6. char letter = 'x';   
  7. printf("string before strnset: %s\n", string);   
  8. strnset(string, letter, 13);   
  9. printf("string after strnset: %s\n", string);   
  10. return 0;   
  11. }  

 

 

17、函数名: strpbrk

功 能: 在串中查找给定字符集中的字符

用 法:

  1. char *strpbrk(char *str1, char *str2);  

 

程序例:

 

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. int main(void)   
  4. {   
  5. char *string1 = "abcdefghijklmnopqrstuvwxyz";   
  6. char *string2 = "onm";   
  7. char *ptr;   
  8. ptr = strpbrk(string1, string2);   
  9. if (ptr)   
  10. printf("strpbrk found first character: %c\n", *ptr);   
  11. else   
  12. printf("strpbrk didn't find character in set\n");   
  13. return 0;   
  14. }  

 

 

18、函数名: strrchr

功 能: 在串中查找指定字符的最后一个出现

用 法:

  1. char *strrchr(char *str, char c);  

 

程序例:

 

  1. #include <string.h>   
  2. #include <stdio.h>   
  3.  
  4. int main(void)   
  5. {   
  6. char string[15];   
  7. char *ptr, c = 'r';   
  8.  
  9. strcpy(string, "This is a string");   
  10. ptr = strrchr(string, c);   
  11. if (ptr)   
  12. printf("The character %c is at position: %d\n", c, ptr-string);   
  13. else   
  14. printf("The character was not found\n");   
  15. return 0;   
  16. }  

 

 

19、函数名: strrev

功 能: 串倒转

用 法:

char *strrev(char *str);

程序例:

 

  1. #include <string.h>   
  2. #include <stdio.h>   
  3. int main(void)   
  4. {   
  5. char *forward = "string";   
  6. printf("Before strrev(): %s\n", forward);   
  7. strrev(forward);   
  8. printf("After strrev(): %s\n", forward);   
  9. return 0;   
  10. }  

 

#p#

20、函数名: strset

功 能: 将一个串中的所有字符都设为指定字符

用 法:

  1. char *strset(char *str, char c);  

 

程序例:

 

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. int main(void)   
  4. {   
  5. char string[10] = "123456789";   
  6. char symbol = 'c';   
  7. printf("Before strset(): %s\n", string);   
  8. strset(string, symbol);   
  9. printf("After strset(): %s\n", string);   
  10. return 0;   
  11. }  

 

 

21、函数名: strspn

功 能: 在串中查找指定字符集的子集的第一次出现

用 法:

  1. int strspn(char *str1, char *str2);  

 

程序例:

 

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include <alloc.h>   
  4. int main(void)   
  5. {   
  6. char *string1 = "1234567890";   
  7. char *string2 = "123DC8";   
  8. int length;   
  9. length = strspn(string1, string2);   
  10. printf("Character where strings differ is at position %d\n", length);   
  11. return 0;   
  12. }  

 

 

22、函数名: strstr

功 能: 在串中查找指定字符串的第一次出现

用 法:

  1. char *strstr(char *str1, char *str2);  

 

程序例:

 

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. int main(void)   
  4. {   
  5. char *str1 = "Borland International", *str2 = "nation", *ptr;   
  6. ptr = strstr(str1, str2);   
  7. printf("The substring is: %s\n", ptr);   
  8. return 0;   
  9. }  

 

 

23、函数名: strtod

功 能: 将字符串转换为double型值

用 法:

  1. double strtod(char *str, char **endptr);  

 

程序例:

 

  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. int main(void)   
  4. {   
  5. char input[80], *endptr;   
  6. double value;   
  7. printf("Enter a floating point number:");   
  8. gets(input);   
  9. value = strtod(input, &endptr);   
  10. printf("The string is %s the number is %lf\n", input, value);   
  11. return 0;   
  12. }  

 

 

24、函数名: strtok

功 能: 查找由在第二个串中指定的分界符分隔开的单词

用 法:

  1. char *strtok(char *str1, char *str2);  

 

程序例:

 

  1. #include <string.h>   
  2. #include <stdio.h>   
  3. int main(void)   
  4. {   
  5. char input[16] = "abc,d";   
  6. char *p;   
  7. /* strtok places a NULL terminator   
  8. in front of the token, if found */   
  9. p = strtok(input, ",");   
  10. if (p) printf("%s\n", p);   
  11. /* A second call to strtok using a NULL   
  12. as the first parameter returns a pointer   
  13. to the character following the token */   
  14. p = strtok(NULL, ",");   
  15. if (p) printf("%s\n", p);   
  16. return 0;   
  17. }  

25、函数名: strtol

功 能: 将串转换为长整数

用 法:

  1. long strtol(char *str, char **endptr, int base);  

 

程序例:

 

  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3. int main(void)   
  4. {   
  5. char *string = "87654321", *endptr;   
  6. long lnumber;   
  7. /* strtol converts string to long integer */   
  8. lnumber = strtol(string, &endptr, 10);   
  9. printf("string = %s long = %ld\n", string, lnumber);   
  10. return 0;   
  11. }  

 

26、函数名: strupr

功 能: 将串中的小写字母转换为大写字母

用 法:

  1. char *strupr(char *str);  

 

程序例:

 

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. int main(void)   
  4. {   
  5. char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;   
  6. /* converts string to upper case characters */   
  7. ptr = strupr(string);   
  8. printf("%s\n", ptr);   
  9. return 0;   
  10. }  

 

 

27、函数名: swab

功 能: 交换字节

用 法:

  1. void swab (char *from, char *to, int nbytes);  

 

程序例:

 

  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3. #include <string.h>   
  4. char source[15] = "rFna koBlrna d";   
  5. char target[15];   
  6. int main(void)   
  7. {   
  8. swab(source, target, strlen(source));   
  9. printf("This is target: %s\n", target);   
  10. return 0;   

到这,关于字符串函数的介绍就给大家介绍完了。希望对你有帮助。

责任编辑:于铁 来源: 互联网
相关推荐

2011-07-15 11:07:41

C语言字符串函数

2010-07-19 15:07:46

Perl字符串处理函数

2021-10-14 15:34:48

C语言字符串函数

2009-08-06 16:01:09

C#字符串函数大全

2014-01-02 16:14:10

PostgreSQL字符串

2022-11-10 07:43:45

2009-09-01 17:41:53

C#截取字符串函数

2010-10-09 11:54:46

MySQL字符串

2010-09-09 11:48:00

SQL函数字符串

2010-02-02 18:01:47

C++字符串替换函数

2010-11-08 17:07:41

SQL Server字

2010-11-26 10:14:40

MySQL repla

2010-07-14 16:35:52

Perl字符串处理函数

2009-11-24 09:55:44

PHP字符串函数

2021-08-20 06:58:31

C++Python函数

2024-02-20 20:12:09

C语言字符串Redis

2023-03-21 15:27:00

RedisC语言字符串

2009-08-07 14:22:56

C#字符串搜索

2009-08-07 14:34:33

C#模式字符串

2009-08-26 13:24:54

C#字符串
点赞
收藏

51CTO技术栈公众号