注册 登录
编程论坛 C语言论坛

在输入的一段文本中查找一个指定的字符串,如果找到,则显示指定的字符串在文本中第一次出现的位置,否则输出没有找到的信息。求解题思路

寜吖 发布于 2020-03-29 14:13, 1432 次点击
#include <stdio.h>
char *str_search(char *s,char *t)
{
    char *ps=s,*pt,*pc;
    while(*ps!='\0')
    {
        for(pt=t,pc=ps;*pt!='\0'&&*pt==*pc;pt++,pc++);
        if(*pt=='\0') return ps;
        ps++;
    }
    return 0;
}
void main()
{
    char s[20],t[5],*p;
    printf("请输入字符串s:");
    scanf("%19s",s);
    printf("请输入字符串t:");
    scanf("%4s",t);
    p=str_search(s,t);
    if(p!=NULL)
        printf("\"%s\"在\"%s\"中第一次出现的位置是%d !\n",t,s,p-s);
    else
        printf("\"%s\"在\"%s\"中没有出现!\n",t,s);
}
1 回复
#2
吹水佬2020-03-29 14:58
C有搜索字符串的函数
1