C语言—strncmp()

作者:追风剑情 发布于:2019-10-30 19:16 分类:C

strncmp()可以只比较字符串的前面几个字符。

示例

  1. //Visual Studio中加上这句才可以使用scanf()
  2. //否则只能使用scanf_s()
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. //引入字符串函数string.h
  7. //一些ANSI之前的系统使用strings.h头文件,而
  8. //有些系统可能根本没有字符串头文件。
  9. #include <string.h>
  10.  
  11. #define LISTSIZE 6
  12.  
  13. //argc: 参数个数 argv[]: 参数数组
  14. int main(int argc, char *argv[])
  15. {
  16. const char * list[LISTSIZE] = {
  17. "astronomy", "astounding",
  18. "astrophysics", "ostracize",
  19. "asterism", "astrophobia"
  20. };
  21. int count = 0;
  22. int i;
  23. for (i = 0; i < LISTSIZE; i++)
  24. {
  25. //只比较前5个字符
  26. if (strncmp(list[i], "astro", 5) == 0)
  27. {
  28. printf("Found: %s\n", list[i]);
  29. count++;
  30. }
  31. }
  32. printf("The list contained %d words beginning"
  33. " with astro.\n", count);
  34.  
  35. system("pause");
  36. return 0;
  37. }

运行测试

1111.png

标签: C语言

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号