求助,初学C语言,请问strcasestr究竟该如何使用?

ak38zzh 2017-02-18 06:58:51

如图,为何他报错?十分感激!!!
...全文
1364 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-02-20
  • 打赏
  • 举报
回复
#include <string.h> char *strstr(const char *haystack, const char *needle); #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <string.h> char *strcasestr(const char *haystack, const char *needle); DESCRIPTION The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating null bytes ('\0') are not compared. The strcasestr() function is like strstr(), but ignores the case of both arguments. RETURN VALUE These functions return a pointer to the beginning of the located substring, or NULL if the substring is not found.
幻夢之葉 2017-02-20
  • 打赏
  • 举报
回复
strcasestr 比 strstr 多了一个忽略大小写的功能吧,怎么用strstr就怎么用strcasestr

char *strcasestr(const char *str1, const char *str2) {
  char *cp = (char *) str1;
  char *s1, *s2;

  if (!*str2) return (char *) str1;

  while (*cp) {
    s1 = cp;
    s2 = (char *) str2;

    while (*s1 && *s2 && !(tolower(*s1) - tolower(*s2))) s1++, s2++;
    if (!*s2) return cp;
    cp++;
  }

  return NULL;
}
小灸舞 版主 2017-02-20
  • 打赏
  • 举报
回复
strcasestr函数

#define _GNU_SOURCE

#include <string.h>

char *strcasestr(const char *haystack, const char *needle);

用于在c串haystack中查找c串needle,忽略大小写。如果找到则返回needle串在haystack串中第一次出现的位置的char指针

在实际的应用中如果只加上头文件,当编译时会出现 warning: assignment makes pointer from integer without a cast

这是因为函数的声明在调用之后。未经声明的函数默认返回int型。

因此要在#include所有头文件之前加 #define _GNU_SOURCE ,以此解决此问题。
  • 打赏
  • 举报
回复
这种东西难得用上,没必要纠结。
ipqtjmqj 2017-02-19
  • 打赏
  • 举报
回复
不是标准的C,必须在linux上,在include前面还要加#define _GNU_SOURCE 参考:http://stackoverflow.com/questions/9935642/how-do-i-use-strcasestr https://linux.die.net/man/3/strcasestr
ak38zzh 2017-02-18
  • 打赏
  • 举报
回复
引用 1 楼 ipqtjmqj 的回复:
你哪里查到有strcasestr这个函数?


我是在浙江大学的mooc上学习的,其中老师提到了一句:


课程链接:http://www.icourse163.org/course/ZJU-1001614008#/info
在字符串一课,9.2》字符串搜索函数。

后来我也查询了这个函数:http://blog.csdn.net/huanggang982/article/details/37901937

但貌似相关资料比较有限
ipqtjmqj 2017-02-18
  • 打赏
  • 举报
回复
你哪里查到有strcasestr这个函数?

70,040

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧