成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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-02-02 18:01:47

C++字符串替換函數

2010-09-09 11:48:00

SQL函數字符串

2010-10-09 11:54:46

MySQL字符串

2009-11-24 09:55:44

PHP字符串函數

2023-03-21 15:27:00

RedisC語言字符串

2024-02-20 20:12:09

C語言字符串Redis

2024-05-30 12:17:25

2010-07-14 16:35:52

Perl字符串處理函數

2010-11-08 17:07:41

SQL Server字

2010-11-26 10:14:40

MySQL repla

2009-08-07 14:46:59

C#匹配字符串

2021-08-20 06:58:31

C++Python函數

2009-08-07 14:15:21

C#字符串分割
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久精品毛片 | 亚洲国产成人精品女人久久久 | 日日操夜夜操天天操 | 亚洲精品一区二区三区中文字幕 | 一区二区三区四区在线视频 | 日韩视频免费看 | 综合另类 | 91av视频在线观看 | 日本字幕在线观看 | 国产高潮好爽受不了了夜色 | 日韩成人精品一区二区三区 | 天堂视频中文在线 | 天天综合网天天综合色 | 亚洲国产精品一区 | 午夜小视频免费观看 | 国产视频中文字幕 | www精品美女久久久tv | 一级片av | 久久av在线播放 | 在线观看亚洲专区 | 一本综合久久 | 亚洲人久久 | 欧美国产视频一区二区 | 国产色在线 | 国产探花在线观看视频 | 在线观看一区 | 亚洲欧洲精品一区 | av日韩在线播放 | 91精品国产综合久久精品 | 久久久国产一区二区三区 | 日韩欧美一区在线 | www.性色| 精品蜜桃一区二区三区 | av一区二区三区四区 | 国产精品一区网站 | 91国自产 | 日本黄色一级视频 | 亚洲天堂色 | 日本高清视频在线播放 | 久久91精品久久久久久9鸭 | 黄色网络在线观看 |