求C语言查找满足条件的程序

来源:百度知道 编辑:UC知道 时间:2024/06/19 15:17:50
编程实现:在给定的字符串中查找满足条件的第一个字符。
要求:
(1)字符串采用初始化的方式处理
(2)通过scanf函数读入一个任意字符
(3)在字符串中查找该字符,如果存在该字符,输出该字符在字符串中的位置以及相应的说明。如果不存在该字符,则要给出相应的信息。
(请给出字符串并给出输入范例~~~)

#include <stdio.h>
main()
{
char str[100],ch;
int i=0;
scanf("%s%c%c",str,&ch,&ch);
while(str[i])
{
if(str[i]==ch)
{
printf("%c在%s的第%d位\n",ch,str,i+1);
return;
}
i++;
}
printf("找不到%c\n",ch);
}

void main
{
char str[]="***********";(*为自己输入的字符串)
char a;
scanf("%c",&a);
int i;
for(i=0;i<=strlen(str);i++)
{
if(a==str[i])
{printf("%c在字符串第%d个位置",a,i+1);break;}
else
printf("字符串中无该字符");
}
}

v1j