使用C编程查找字符串中子字符串的出现次数

时间:2019-09-27 06:23:40

标签: c

我正在尝试使用c程序读取包含字符数组或字符串的文本文件,并找到称为“ GLROX”的子字符串的出现次数,并说出找到后的顺序。 “ inputGLORX.txt”中包含以下字符串。

GLAAAROBBBBBBXGLROXGLROXGLROXGLROXGLCCCCCCCCCCCCCCROXGGLROXGLROXGLROXGLROXGLROXGLROXGLROXGLROXGLROXGLROXGLROX

但是我得到的结果很奇怪。如果某些C编程专家能够帮助我解决这个问题,并预先感谢,那就太好了。

#include <stdio.h>
#include <conio.h>
#include <string.h>
#define NUMBER_OF_STRINGS 40
#define MAX_STRING_SIZE 7
void seqFound()
{
    printf("Sequence Found\n");
}

int main()
{
    FILE *fp;
    char buff[1000];
    char strptrArr[NUMBER_OF_STRINGS] [MAX_STRING_SIZE];
    const char *search = "GLROX";
    fp = fopen("D:/CandC++/inputGLORX.txt", "r");

    if(fp==NULL)
        printf("It is a null pointer");

    while(!feof(fp))
    {
      //fscanf(fp, "%s", buff);
      fgets(buff, 1000,fp);
    }

    int len = strlen(buff);
    printf("length is %d\n",len);
    int count = 0;
    char *store;

    while(store = strstr(buff, search))
    {
       printf("substring is %s \n",store);
       count++;
       search++;
    }

    printf("count is %d\n",count);
    while (count!=0) {
        seqFound();
        count--;
    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

如评论中所述,它们至少是代码中的2个问题:您的fgets仅会获取最后一行(如果它根本无法获取最后一行?无论如何,这不是您想要的) ,而您正在递增search字符串而不是buff字符串。

只要文件中的任何行都不超过999个字符,这种情况就可以解决大多数问题。如果您在搜索字符串中使用\n或NULL字符,则此方法将无法正常工作。

int count = 0;
while (fgets(buff, 1000, fp) != NULL)
{
    char *temp = buff;
    while ((temp = strstr(temp, search)))
    {
        printf("%d. %s\n", count + 1, temp);
        count++;
        temp++;
    }
}

这里是测试的主要工具。我使用argv提供了input.txtsearch字符串。

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    FILE    *fp;
    char    buff[1000];
    char    *search;

    if (argc < 3)
        return (-1);
    search = argv[2];
    if (search[0] == '\0')
        return (-1);
    if ((fp = fopen(argv[1], "r")) == NULL)
        return (-1);
    int count = 0;
    while (fgets(buff, 1000, fp) != NULL)
    {
        char *temp = buff;
        while ((temp = strstr(temp, search)))
        {
            printf("%d. %s\n", count + 1, temp);
            count++;
            temp++;
        }
    }
    printf("Match found: %d\n", count);
    return 0;
}

答案 1 :(得分:1)

buff中搜索的方式是错误的,即此代码:

while(store = strstr(buff, search))
{
   printf("substring is %s \n",store);
   count++;
   search++;  // <------- ups
}

点击时,您可以更改search,即您要查找的字符串。那不是你想要的。搜索字符串(也就是针)应始终保持相同。相反,您希望在缓冲区buff中向前移动,以便可以在缓冲区的其余部分中进行搜索。

可能是这样的:

int main()
{

  const char* buff = "GLAAAROBBBBBBXGLROXGLROXGLROXGLROXGLCCCCCCCCCCCCCCROXGGLROXGLROXGLROXGLROXGLROXGLROXGLROXGLROXGLROXGLROXGLROX";

  const char* search = "GLROX";
  const char* remBuff = buff;    // Pointer to the remainder of buff
                                 // Initialized to be the whole buffer
  const char* hit;
  int cnt = 0;
  while((hit = strstr(remBuff, search)))  // Search in the remainder of buff
  {
    ++cnt;
    remBuff = hit + 1;    // Update the remainder pointer so it points just 1 char
                          // after the current hit
  }
  printf("Found substring %d times\n", cnt);
  return 0;
}

输出:

Found substring 15 times