编写一个用于在字符串中查找某字符发函数

#include<iostream>
#include<cstdio>
using namespace std;
char *p;
char a[1000];
int len;
int s=0;
int on=0;
void work(char *p,char t)
{
    int i;
    for(i=0;i<len;i++)
    {
        if(*p==t&&on==0)
        {
            s=i+1;
            on=1;
        }
        p++;
    }
}
int main()
{
    gets(a);
    char t;
    len=strlen(a);
    scanf("%c",&t);
    p=a;
    work(p,t);
    if(s!=0)
    printf("%d",s);
    else
        cout<<"no";
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/qq_42552468/article/details/81347483