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. }  

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;   

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值