飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 4763|回复: 10

C语言字符串函数大全

[复制链接]

该用户从未签到

发表于 2018-2-6 11:55:04 | 显示全部楼层 |阅读模式



  1. 函数名: stpcpy
  2. 功 能: 拷贝一个字符串到另一个
  3. 用 法: char *stpcpy(char *destin, char *source);
  4. 程序例:

  5. #include <stdio.h>
  6. #include <string.h>

  7. int main(void)
  8. {
  9.     char string[10];
  10.     char *str1 = "abcdefghi";

  11.     stpcpy(string, str1);
  12.     printf("%s\n", string);
  13.     return 0;
  14. }

  15. 函数名: strcat
  16. 功 能: 字符串拼接函数
  17. 用 法: char *strcat(char *destin, char *source);
  18. 程序例:

  19. #include <string.h>
  20. #include <stdio.h>

  21. int main(void)
  22. {
  23.     char destination[25];
  24.     char *blank = " ", *c = "C++", *Borland = "Borland";

  25.     strcpy(destination, Borland);
  26.     strcat(destination, blank);
  27.     strcat(destination, c);

  28.     printf("%s\n", destination);
  29.     return 0;
  30. }

  31. 函数名: strchr
  32. 功 能: 在一个串中查找给定字符的第一个匹配之处
  33. 用 法: char *strchr(char *str, char c);
  34. 程序例:

  35. #include <string.h>
  36. #include <stdio.h>

  37. int main(void)
  38. {
  39.     char string[15];
  40.     char *ptr, c = ‘r‘;

  41.     strcpy(string, "This is a string");
  42.     ptr = strchr(string, c);
  43.     if (ptr)
  44.     printf("The character %c is at position: %d\n", c, ptr-string);
  45.     else
  46.     printf("The character was not found\n");
  47.     return 0;
  48. }

  49. 函数名: strcmp
  50. 功 能: 串比较
  51. 用 法: int strcmp(char *str1, char *str2);
  52. 看Asic码,str1>str2,返回值 > 0;两串相等,返回0
  53. 程序例:

  54. #include <string.h>
  55. #include <stdio.h>

  56. int main(void)
  57. {
  58.     char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
  59.     int ptr;

  60.     ptr = strcmp(buf2, buf1);
  61.     if (ptr > 0)
  62.     printf("buffer 2 is greater than buffer 1\n");
  63.     else
  64.     printf("buffer 2 is less than buffer 1\n");

  65.     ptr = strcmp(buf2, buf3);
  66.     if (ptr > 0)
  67.     printf("buffer 2 is greater than buffer 3\n");
  68.     else
  69.     printf("buffer 2 is less than buffer 3\n");

  70.     return 0;
  71. }

  72. 函数名: strncmpi
  73. 功 能: 将一个串中的一部分与另一个串比较, 不管大小写
  74. 用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
  75. 程序例:

  76. #include <string.h>
  77. #include <stdio.h>

  78. int main(void)
  79. {
  80.     char *buf1 = "BBB", *buf2 = "bbb";
  81.     int ptr;

  82.     ptr = strcmpi(buf2, buf1);

  83.     if (ptr > 0)
  84.     printf("buffer 2 is greater than buffer 1\n");

  85.     if (ptr < 0)
  86.     printf("buffer 2 is less than buffer 1\n");

  87.     if (ptr == 0)
  88.     printf("buffer 2 equals buffer 1\n");

  89.     return 0;
  90. }

  91. 函数名: strcpy
  92. 功 能: 串拷贝
  93. 用 法: char *strcpy(char *str1, char *str2);
  94. 程序例:

  95. #include <stdio.h>
  96. #include <string.h>

  97. int main(void)
  98. {
  99.     char string[10];
  100.     char *str1 = "abcdefghi";

  101.     strcpy(string, str1);
  102.     printf("%s\n", string);
  103.     return 0;
  104. }

  105. 函数名: strcspn
  106. 功 能: 在串中查找第一个给定字符集内容的段
  107. 用 法: int strcspn(char *str1, char *str2);
  108. 程序例:

  109. #include <stdio.h>
  110. #include <string.h>
  111. #include <alloc.h>

  112. int main(void)
  113. {
  114.     char *string1 = "1234567890";
  115.     char *string2 = "747DC8";
  116.     int length;

  117.     length = strcspn(string1, string2);
  118.     printf("Character where strings intersect is at position %d\n", length);

  119.     return 0;
  120. }

  121. 函数名: strdup
  122. 功 能: 将串拷贝到新建的位置处
  123. 用 法: char *strdup(char *str);
  124. 程序例:

  125. #include <stdio.h>
  126. #include <string.h>
  127. #include <alloc.h>

  128. int main(void)
  129. {
  130.     char *dup_str, *string = "abcde";

  131.     dup_str = strdup(string);
  132.     printf("%s\n", dup_str);
  133.     free(dup_str);

  134.     return 0;
  135. }

  136. 函数名: stricmp
  137. 功 能: 以大小写不敏感方式比较两个串
  138. 用 法: int stricmp(char *str1, char *str2);
  139. 程序例:

  140. #include <string.h>
  141. #include <stdio.h>

  142. int main(void)
  143. {
  144.     char *buf1 = "BBB", *buf2 = "bbb";
  145.     int ptr;

  146.     ptr = stricmp(buf2, buf1);

  147.     if (ptr > 0)
  148.     printf("buffer 2 is greater than buffer 1\n");

  149.     if (ptr < 0)
  150.     printf("buffer 2 is less than buffer 1\n");

  151.     if (ptr == 0)
  152.     printf("buffer 2 equals buffer 1\n");

  153.     return 0;
  154. }

  155. 函数名: strerror
  156. 功 能: 返回指向错误信息字符串的指针
  157. 用 法: char *strerror(int errnum);
  158. 程序例:

  159. #include <stdio.h>
  160. #include <errno.h>

  161. int main(void)
  162. {
  163.     char *buffer;
  164.     buffer = strerror(errno);
  165.     printf("Error: %s\n", buffer);
  166.     return 0;
  167. }

  168. 函数名: strcmpi
  169. 功 能: 将一个串与另一个比较, 不管大小写
  170. 用 法: int strcmpi(char *str1, char *str2);
  171. 程序例:

  172. #include <string.h>
  173. #include <stdio.h>

  174. int main(void)
  175. {
  176.     char *buf1 = "BBB", *buf2 = "bbb";
  177.     int ptr;

  178.     ptr = strcmpi(buf2, buf1);

  179.     if (ptr > 0)
  180.     printf("buffer 2 is greater than buffer 1\n");

  181.     if (ptr < 0)
  182.     printf("buffer 2 is less than buffer 1\n");

  183.     if (ptr == 0)
  184.     printf("buffer 2 equals buffer 1\n");

  185.     return 0;
  186. }

  187. 函数名: strncmp
  188. 功 能: 串比较
  189. 用 法: int strncmp(char *str1, char *str2, int maxlen);
  190. 程序例:

  191. #include <string.h>
  192. #include <stdio.h>

  193. int main(void)

  194. {
  195.     char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
  196.     int ptr;

  197.     ptr = strncmp(buf2,buf1,3);
  198.     if (ptr > 0)
  199.     printf("buffer 2 is greater than buffer 1\n");
  200.     else
  201.     printf("buffer 2 is less than buffer 1\n");

  202.     ptr = strncmp(buf2,buf3,3);
  203.     if (ptr > 0)
  204.     printf("buffer 2 is greater than buffer 3\n");
  205.     else
  206.     printf("buffer 2 is less than buffer 3\n");

  207.     return(0);
  208. }

  209. 函数名: strncmpi
  210. 功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写
  211. 用 法: int strncmpi(char *str1, char *str2);
  212. 程序例:

  213. #include <string.h>
  214. #include <stdio.h>

  215. int main(void)
  216. {
  217.     char *buf1 = "BBBccc", *buf2 = "bbbccc";
  218.     int ptr;

  219.     ptr = strncmpi(buf2,buf1,3);

  220.     if (ptr > 0)
  221.     printf("buffer 2 is greater than buffer 1\n");

  222.     if (ptr < 0)
  223.     printf("buffer 2 is less than buffer 1\n");

  224.     if (ptr == 0)
  225.     printf("buffer 2 equals buffer 1\n");

  226.     return 0;
  227. }

  228. 函数名: strncpy
  229. 功 能: 串拷贝
  230. 用 法: char *strncpy(char *destin, char *source, int maxlen);
  231. 程序例:

  232. #include <stdio.h>
  233. #include <string.h>

  234. int main(void)
  235. {
  236.     char string[10];
  237.     char *str1 = "abcdefghi";

  238.     strncpy(string, str1, 3);
  239.     string[3] = ‘\0‘;
  240.     printf("%s\n", string);
  241.     return 0;
  242. }

  243. 函数名: strnicmp
  244. 功 能: 不注重大小写地比较两个串
  245. 用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);
  246. 程序例:

  247. #include <string.h>
  248. #include <stdio.h>

  249. int main(void)
  250. {
  251.     char *buf1 = "BBBccc", *buf2 = "bbbccc";
  252.     int ptr;

  253.     ptr = strnicmp(buf2, buf1, 3);

  254.     if (ptr > 0)
  255.     printf("buffer 2 is greater than buffer 1\n");

  256.     if (ptr < 0)
  257.     printf("buffer 2 is less than buffer 1\n");

  258.     if (ptr == 0)
  259.     printf("buffer 2 equals buffer 1\n");

  260.     return 0;
  261. }

  262. 函数名: strnset
  263. 功 能: 将一个串中的所有字符都设为指定字符
  264. 用 法: char *strnset(char *str, char ch, unsigned n);
  265. 程序例:

  266. #include <stdio.h>
  267. #include <string.h>

  268. int main(void)
  269. {
  270.     char *string = "abcdefghijklmnopqrstuvwxyz";
  271.     char letter = ‘x‘;

  272.     printf("string before strnset: %s\n", string);
  273.     strnset(string, letter, 13);
  274.     printf("string after  strnset: %s\n", string);

  275.     return 0;
  276. }

  277. 函数名: strpbrk
  278. 功 能: 在串中查找给定字符集中的字符
  279. 用 法: char *strpbrk(char *str1, char *str2);
  280. 程序例:

  281. #include <stdio.h>
  282. #include <string.h>

  283. int main(void)
  284. {
  285.     char *string1 = "abcdefghijklmnopqrstuvwxyz";
  286.     char *string2 = "onm";
  287.     char *ptr;

  288.     ptr = strpbrk(string1, string2);

  289.     if (ptr)
  290.     printf("strpbrk found first character: %c\n", *ptr);
  291.     else
  292.     printf("strpbrk didn‘t find character in set\n");

  293.     return 0;
  294. }

  295. 函数名: strrchr
  296. 功 能: 在串中查找指定字符的最后一个出现
  297. 用 法: char *strrchr(char *str, char c);
  298. 程序例:

  299. #include <string.h>
  300. #include <stdio.h>

  301. int main(void)
  302. {
  303.     char string[15];
  304.     char *ptr, c = ‘r‘;

  305.     strcpy(string, "This is a string");
  306.     ptr = strrchr(string, c);
  307.     if (ptr)
  308.     printf("The character %c is at position: %d\n", c, ptr-string);
  309.     else
  310.     printf("The character was not found\n");
  311.     return 0;
  312. }

  313. 函数名: strrev
  314. 功 能: 串倒转
  315. 用 法: char *strrev(char *str);
  316. 程序例:

  317. #include <string.h>
  318. #include <stdio.h>

  319. int main(void)
  320. {
  321.     char *forward = "string";

  322.     printf("Before strrev(): %s\n", forward);
  323.     strrev(forward);
  324.     printf("After strrev():  %s\n", forward);
  325.     return 0;
  326. }

  327. 函数名: strset
  328. 功 能: 将一个串中的所有字符都设为指定字符
  329. 用 法: char *strset(char *str, char c);
  330. 程序例:

  331. #include <stdio.h>
  332. #include <string.h>

  333. int main(void)
  334. {
  335.     char string[10] = "123456789";
  336.     char symbol = ‘c‘;

  337.     printf("Before strset(): %s\n", string);
  338.     strset(string, symbol);
  339.     printf("After strset():  %s\n", string);
  340.     return 0;
  341. }

  342. 函数名: strspn
  343. 功 能: 在串中查找指定字符集的子集的第一次出现
  344. 用 法: int strspn(char *str1, char *str2);
  345. 程序例:

  346. #include <stdio.h>
  347. #include <string.h>
  348. #include <alloc.h>

  349. int main(void)
  350. {
  351.     char *string1 = "1234567890";
  352.     char *string2 = "123DC8";
  353.     int length;

  354.     length = strspn(string1, string2);
  355.     printf("Character where strings differ is at position %d\n", length);
  356.     return 0;
  357. }

  358. 函数名: strstr
  359. 功 能: 在串中查找指定字符串的第一次出现
  360. 用 法: char *strstr(char *str1, char *str2);
  361. 程序例:

  362. #include <stdio.h>
  363. #include <string.h>

  364. int main(void)
  365. {
  366.     char *str1 = "Borland International", *str2 = "nation", *ptr;

  367.     ptr = strstr(str1, str2);
  368.     printf("The substring is: %s\n", ptr);
  369.     return 0;
  370. }

  371. 函数名: strtod
  372. 功 能: 将字符串转换为double型值
  373. 用 法: double strtod(char *str, char **endptr);
  374. 程序例:

  375. #include <stdio.h>
  376. #include <stdlib.h>

  377. int main(void)
  378. {
  379.     char input[80], *endptr;
  380.     double value;

  381.     printf("Enter a floating point number:");
  382.     gets(input);
  383.     value = strtod(input, &endptr);
  384.     printf("The string is %s the number is %lf\n", input, value);
  385.     return 0;
  386. }

  387. 函数名: strtok
  388. 功 能: 查找由在第二个串中指定的分界符分隔开的单词
  389. 用 法: char *strtok(char *str1, char *str2);
  390. 程序例:

  391. #include <string.h>
  392. #include <stdio.h>

  393. int main(void)
  394. {
  395.     char input[16] = "abc,d";
  396.     char *p;

  397.     /* strtok places a NULL terminator
  398.      in front of the token, if found */
  399.     p = strtok(input, ",");
  400.     if (p) printf("%s\n", p);

  401.     /* A second call to strtok using a NULL
  402.      as the first parameter returns a pointer
  403.      to the character following the token  */
  404.     p = strtok(NULL, ",");
  405.     if (p) printf("%s\n", p);
  406.     return 0;
  407. }

  408. 函数名: strtol
  409. 功 能: 将串转换为长整数
  410. 用 法: long strtol(char *str, char **endptr, int base);
  411. 程序例:

  412. #include <stdlib.h>
  413. #include <stdio.h>

  414. int main(void)
  415. {
  416.     char *string = "87654321", *endptr;
  417.     long lnumber;

  418.     /* strtol converts string to long integer  */
  419.     lnumber = strtol(string, &endptr, 10);
  420.     printf("string = %s  long = %ld\n", string, lnumber);

  421.     return 0;
  422. }

  423. 函数名: strupr
  424. 功 能: 将串中的小写字母转换为大写字母
  425. 用 法: char *strupr(char *str);
  426. 程序例:

  427. #include <stdio.h>
  428. #include <string.h>

  429. int main(void)
  430. {
  431.     char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;

  432.     /* converts string to upper case characters */
  433.     ptr = strupr(string);
  434.     printf("%s\n", ptr);
  435.     return 0;
  436. }

  437. 函数名: swab
  438. 功 能: 交换字节
  439. 用 法: void swab (char *from, char *to, int nbytes);
  440. 程序例:

  441. #include <stdlib.h>
  442. #include <stdio.h>
  443. #include <string.h>

  444. char source[15] = "rFna koBlrna d";
  445. char target[15];

  446. int main(void)
  447. {
  448.     swab(source, target, strlen(source));
  449.     printf("This is target: %s\n", target);
  450.     return 0;
  451. }
复制代码

评分

参与人数 2威望 +24 飘云币 +20 收起 理由
不破不立 + 20 + 20 PYG有你更精彩!
Master.lu + 4 感谢发布原创作品!

查看全部评分

本帖被以下淘专辑推荐:

PYG19周年生日快乐!
  • TA的每日心情

    3 小时前
  • 签到天数: 2578 天

    [LV.Master]伴坛终老

    发表于 2018-2-6 13:04:06 | 显示全部楼层
    谢谢NISY,沙发!!!
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2021-10-30 00:03
  • 签到天数: 388 天

    [LV.9]以坛为家II

    发表于 2018-2-6 13:38:24 | 显示全部楼层
    学习了,谢谢分享!
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-2-24 23:52
  • 签到天数: 323 天

    [LV.8]以坛为家I

    发表于 2018-2-6 19:39:28 | 显示全部楼层
    不错,谢谢楼主………………
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    昨天 20:19
  • 签到天数: 1809 天

    [LV.Master]伴坛终老

    发表于 2018-2-6 19:45:09 | 显示全部楼层
      谢谢校长的提供好好学习
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2019-11-28 11:49
  • 签到天数: 11 天

    [LV.3]偶尔看看II

    发表于 2018-2-8 12:15:10 | 显示全部楼层
    为啥不做个附件呢?
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    3 小时前
  • 签到天数: 2164 天

    [LV.Master]伴坛终老

    发表于 2018-2-8 12:43:25 | 显示全部楼层
    正好来学习 一下
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2023-5-25 11:16
  • 签到天数: 25 天

    [LV.4]偶尔看看III

    发表于 2018-8-5 13:57:28 | 显示全部楼层
    感谢校长在这儿收集分享一份,以后可以直接在这儿搜索了。
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表