在c中查找字符串中是否含有特定字符
可以使用标准库函数strchr()来查找字符串中是否含有特定字符。该函数的原型为:
char *strchr(const char *str, int c);
其中,第一个参数是要查找的字符串,第二个参数是要查找的字符。函数返回一个指向第一次出现该字符的位置的指针,如果没找到则返回NULL。
下面是一个示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello, world";
char ch = 'l';
if (strchr(str, ch) != NULL) {
printf("'%c' is found in '%s'\n", ch, str);
} else {
printf("'%c' is not found in '%s'\n", ch, str);
}
return 0;
}
输出结果为:
'l' is found in 'hello, world'
原文地址: https://www.cveoy.top/t/topic/bcxY 著作权归作者所有。请勿转载和采集!