题目:
编写一个名为is_within()函数,接受一个字符和一个指向字符串的指针作为两个函数形参。
如果指定字符在字符串中,该函数返回一个非零值,否则,返回0
源代码:
#include <stdio.h>
#include <string.h>
int is_within(char * st,char ch);
int main(){
char st[40] = "hello world";
char ch=0;
do{
printf("请输入需要查找的字符(输入1退出): ");
scanf("%c",&ch);
if(is_within(st,ch)) printf("字符 %c 存在\n",ch);
else{
printf("字符 %c 不存在\n",ch);
break;
}
getchar();
}while(1);
return 0;
}
int is_within(char * st,char ch){
int i = 0;
int max = strlen(st)/strlen(st+i);
while(st[i] != ch && i < max) i++;
if(st[i] == ch) return 1;
else return 0;
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈