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

Posted

技术标签:

【中文标题】在C中查找字符串中子字符串的位置【英文标题】:In C find position of substring in a string 【发布时间】:2012-08-03 21:51:20 【问题描述】:

这是一个接受a的程序:

    来自用户的一句话。 用户的话。

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

#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;

【问题讨论】:

您可以减去 2 个指针(指向 char)并将结果解释为整数:position = ptr - sntnc; 不要使用gets()!不要fflush()输入流! 在 Java / javascript 中,我们正好有你需要的函数:indexOf。然而,通过快速搜索,我找到了一个讨论您需要什么的线程:C 中的类似 indexOf 函数,请查看这篇文章:***.com/questions/4824/string-indexof-function-in-c 【参考方案1】:

你可以使用这个简单的strpos修改

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int strpos(char *haystack, char *needle, int offset);
int main()

    char *p = "Hello there all y'al, hope that you are all well";
    int pos = strpos(p, "all", 0);
    printf("First all at : %d\n", pos);
    pos = strpos(p, "all", 10);
    printf("Second all at : %d\n", pos);



int strpos(char *hay, char *needle, int offset)

   char haystack[strlen(hay)];
   strncpy(haystack, hay+offset, strlen(hay)-offset);
   char *p = strstr(haystack, needle);
   if (p)
      return p - haystack+offset;
   return -1;

【讨论】:

【参考方案2】:

我对这个帖子中原始帖子的评论: 此声明不正确:

    char sntnc[50], word[50], *ptr[50];

C 代码甚至无法编译:它将在这一行失败:

    ptr = strstr(sntnc,word);

所以该行应改为:

   char sntnc[50], word[50], *ptr;

而且你不需要分配给'ptr string'的内存。你只需要一个指向 char 的指针。

【讨论】:

【参考方案3】:

由于某些原因,我在使用 strstr() 时遇到了问题,我还想要 index.html

我做了这个函数来查找子字符串在更大字符串中的位置(如果存在),否则返回 -1。

 int isSubstring(char * haystack, char * needle) 
     int i = 0;
     int d = 0;
     if (strlen(haystack) >= strlen(needle)) 
         for (i = strlen(haystack) - strlen(needle); i >= 0; i--) 
             int found = 1; //assume we found (wanted to use boolean)
             for (d = 0; d < strlen(needle); d++) 
                 if (haystack[i + d] != needle[d]) 
                     found = 0; 
                     break;
                 
             
             if (found == 1) 
                 return i;
             
         
         return -1;
      else 
         //fprintf(stdout, "haystack smaller\n"); 
     
  

【讨论】:

【参考方案4】:

仅供参考:

char saux[] = "this is a string, try to search_this here";
int dlenstr = strlen(saux);
if (dlenstr > 0)

    char *pfound = strstr(saux, "search_this"); //pointer to the first character found 's' in the string saux
    if (pfound != NULL)
    
        int dposfound = int (pfound - saux); //saux is already pointing to the first string character 't'.
    

【讨论】:

【参考方案5】:

strstr() 的返回是指向你的“单词”第一次出现的指针,所以

pos=ptr-sntc;

这只是因为 sntc 和 ptr 是指向同一个字符串的指针。为了澄清当我说发生时,它是在您的目标字符串中找到匹配字符串时第一个匹配字符的位置。

【讨论】:

【参考方案6】:

ptr 指针将指向word 的开头,因此您可以从中减去句子指针sntnc 的位置:

pos = ptr - sntnc;

【讨论】:

...但前提是ptr 不是NULL ptr 是 char** 指针,sntnc 是 char* 指针。我们如何减去它们?

以上是关于在C中查找字符串中子字符串的位置的主要内容,如果未能解决你的问题,请参考以下文章

C语言--编程实现字符串中子串的查找

查找字符串中子字符串最后一次出现的索引

如何在字符串中查找子字符串,其中子字符串位于 Access Query 的单独表中?

Java中字符串indexof() 的使用方法

Java中字符串indexof() 的使用方法

C语言 计算字符串中子串出现的次数 求更改