龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

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

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
20、函数名: strset 功 能: 将一个串中的所有字符都设为指定字符 用 法: char *strset( char *str, char c); 程序例: #includestdio.h #includestring.h int main( void ) { char strin

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(): %sn", string);   
  8. strset(string, symbol);   
  9. printf("After strset(): %sn", 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 %dn", 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: %sn", 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 %lfn", 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("%sn", 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("%sn", 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 = %ldn", 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("%sn", 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: %sn", target);   
  10. return 0;   

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

精彩图集

赞助商链接