C语言编程如何查找字符串中指定汉字?

Python09

C语言编程如何查找字符串中指定汉字?,第1张

错误在于你判断了第一个非@字符时就已经输出没有字符@退出循环了所以不会检测@了。改成下面就行了:

#include

#include

int

main()

{

char

sh[100],n=0

gets(sh)

for(int

i=0sh[i]i

)

if(sh[i]=='@')

n

if(n==0)

printf("没有字符

@\n")

else

printf("有字符

@\n")

}

C语言中的标准函数库中的strchr()函数可以实现查找字符串中的某个字符。头文件: #include <string.h>函数原型:char *strchr(const char *s, int c)函数说明:从左向右,在字符串s中查找字符c首次出现的位置,如果找到返回c在s中的位置(指针),否则返回NULL例:<pre t="code" l="cpp">#include <stdio.h>

#include <string.h>

void main()

{

char str[]="hello world"

char *p=strchr(str, 'w')

if ( p )

printf("find 'w'!)

else

printf("not found!")

}相关函数:char *strrchr(const char *s, int c)从右向左,查找s中最右边的匹配字符位置char *strstr(const char *s, const char *sub)//在s中查找sub子串出现的位置

#include<stdio.h>

#include<string.h>

int main()

{char s[200],s1[3]

 int i

 printf("请输入一串汉字:")

 scanf("%s",s)

 printf("请输入要查找的一个汉字:")

 scanf("%s",s1)

 for(i=0i<strlen(s)i+=2)

   if(s[i]==s1[0]&&s[i+1]==s1[1])

     {printf("您输入的第%d个汉字是%s\n",i/2+1,s1)

      break

     }

 if(i>=strlen(s))printf("未找到!"); 

 return 0

}