小编Mug*_*mbo的帖子

在C中查找字符串中子字符串的位置

这是一个接受的程序:

  1. 来自用户的句子.
  2. 用户的话.

如何找到句子中输入单词的位置?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char sntnc[50], word[50], *ptr[50];
    int pos;
    puts("\nEnter a sentence");
    gets(sntnc);
    fflush(stdin);
    puts("\nEnter a word");
    gets(word);
    fflush(stdin);
    ptr=strstr(sntnc,word);

    //how do I find out at what position the word occurs in the sentence?

    //Following is the required output
    printf("The word starts at position #%d", pos);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c string substring strstr

7
推荐指数
2
解决办法
6万
查看次数

标签 统计

c ×1

string ×1

strstr ×1

substring ×1