自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C語言字符串函數(shù)集錦(二)

開發(fā) 后端
本文介紹的是C語言中的字符串函數(shù),每個函數(shù)都介紹了用法,并舉例介紹的。希望對大家有幫助,一起來看。

說起字符串函數(shù),我想大家都不陌生。字符串函數(shù)對二進制數(shù)據(jù)、字符串和表達式執(zhí)行不同的運算。下面總結(jié)了C語言中的字符串函數(shù)。

13、函數(shù)名: 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、函數(shù)名: 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、函數(shù)名: 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、函數(shù)名: strnset

功 能: 將一個串中的所有字符都設(shè)為指定字符

用 法:

  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、函數(shù)名: 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、函數(shù)名: strrchr

功 能: 在串中查找指定字符的最后一個出現(xiàn)

用 法:

  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、函數(shù)名: strrev

功 能: 串倒轉(zhuǎn)

用 法:

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、函數(shù)名: strset

功 能: 將一個串中的所有字符都設(shè)為指定字符

用 法:

  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、函數(shù)名: strspn

功 能: 在串中查找指定字符集的子集的第一次出現(xiàn)

用 法:

  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、函數(shù)名: strstr

功 能: 在串中查找指定字符串的第一次出現(xiàn)

用 法:

  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、函數(shù)名: strtod

功 能: 將字符串轉(zhuǎn)換為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、函數(shù)名: 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、函數(shù)名: strtol

功 能: 將串轉(zhuǎn)換為長整數(shù)

用 法:

  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、函數(shù)名: strupr

功 能: 將串中的小寫字母轉(zhuǎn)換為大寫字母

用 法:

  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、函數(shù)名: swab

功 能: 交換字節(jié)

用 法:

  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;   

到這,關(guān)于字符串函數(shù)的介紹就給大家介紹完了。希望對你有幫助。

責(zé)任編輯:于鐵 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-15 11:07:41

C語言字符串函數(shù)

2010-07-19 15:07:46

Perl字符串處理函數(shù)

2021-10-14 15:34:48

C語言字符串函數(shù)

2009-08-06 16:01:09

C#字符串函數(shù)大全

2014-01-02 16:14:10

PostgreSQL字符串

2022-11-10 07:43:45

2009-09-01 17:41:53

C#截取字符串函數(shù)

2010-09-09 11:48:00

SQL函數(shù)字符串

2010-02-02 18:01:47

C++字符串替換函數(shù)

2010-10-09 11:54:46

MySQL字符串

2009-11-24 09:55:44

PHP字符串函數(shù)

2023-03-21 15:27:00

RedisC語言字符串

2024-02-20 20:12:09

C語言字符串Redis

2010-11-08 17:07:41

SQL Server字

2010-11-26 10:14:40

MySQL repla

2010-07-14 16:35:52

Perl字符串處理函數(shù)

2021-08-20 06:58:31

C++Python函數(shù)

2024-05-30 12:17:25

2010-02-04 10:52:36

C++字符串分割函數(shù)

2009-08-07 14:46:59

C#匹配字符串
點贊
收藏

51CTO技術(shù)棧公眾號