C 中的子字符串计数

发布于 2024-12-07 10:23:39 字数 692 浏览 0 评论 0原文

我的一个朋友需要帮助计算字符串中子字符串的出现次数,我想出了以下代码。有谁知道更好的方法来做到这一点?

#include "stdio.h"
#include "string.h"

int main(int argc, char *argv[])
{
    char str1[50], str2[50];
    int i, j, l1, l2, match, count;

    printf("String 1:\n");
    gets(str2);
    printf("String 2:\n");
    gets(str1);

    l1 = strlen(str1);
    l2 = strlen(str2);

    count = 0;

    for(i = 0; i < l1; i++)
    {
        match = 0;
        for(j = 0; j < l2; j++)
        {
            if(str1[i + j] == str2[j])
            {
                match++;
            }
        }

        if(match == l2)
        {
            count++;
        }
    }

    printf("Substrings: %d\n", count);
}

A friend of mine needed help counting the occurrences of a substring in a string, and I came up with the following code. Does anyone know a better method to do this?

#include "stdio.h"
#include "string.h"

int main(int argc, char *argv[])
{
    char str1[50], str2[50];
    int i, j, l1, l2, match, count;

    printf("String 1:\n");
    gets(str2);
    printf("String 2:\n");
    gets(str1);

    l1 = strlen(str1);
    l2 = strlen(str2);

    count = 0;

    for(i = 0; i < l1; i++)
    {
        match = 0;
        for(j = 0; j < l2; j++)
        {
            if(str1[i + j] == str2[j])
            {
                match++;
            }
        }

        if(match == l2)
        {
            count++;
        }
    }

    printf("Substrings: %d\n", count);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤凫 2024-12-14 10:23:40

不要使用或鼓励使用gets。除了它在代码中引入故障点这一事实之外,它已从 C99 起被弃用,并将从 C1X 中完全消失。

正如其他人所说,strstr 是你的朋友:

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

int main(void)
{
  char s1[50], s2[50];
  char *p;
  size_t count = 0;
  size_t len1;

  printf("Gimme a string: ");
  fflush(stdout);
  fgets(s1, sizeof s1, stdin);
  p = strchr(s1, '\n');          // get rid of the trailing newline
  if (p)
    *p = 0;

  printf("Gimme another string: ");
  fflush(stdout);
  fgets(s2, sizeof s2, stdin);
  p = strchr(s2, '\n');          // get rid of the trailing newline
  if (p)
    *p = 0;

  p = s2;
  len1 = strlen(s1);

  while ((p = strstr(p, s1)) != NULL && p != s1)
  {
    count++;
    p += len1;
  }

  printf("Found %lu occurrences of %s in %s\n", count, s1, s2);
  return 0;
}

Please do not use or encourage the use of gets. Beyond the fact that it will introduce a point of failure in your code, it has been deprecated as of C99 and will be gone completely from C1X.

As others have said, strstr is your friend here:

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

int main(void)
{
  char s1[50], s2[50];
  char *p;
  size_t count = 0;
  size_t len1;

  printf("Gimme a string: ");
  fflush(stdout);
  fgets(s1, sizeof s1, stdin);
  p = strchr(s1, '\n');          // get rid of the trailing newline
  if (p)
    *p = 0;

  printf("Gimme another string: ");
  fflush(stdout);
  fgets(s2, sizeof s2, stdin);
  p = strchr(s2, '\n');          // get rid of the trailing newline
  if (p)
    *p = 0;

  p = s2;
  len1 = strlen(s1);

  while ((p = strstr(p, s1)) != NULL && p != s1)
  {
    count++;
    p += len1;
  }

  printf("Found %lu occurrences of %s in %s\n", count, s1, s2);
  return 0;
}
呆头 2024-12-14 10:23:40

您可能想看看 strstr 函数(如果您还不熟悉它)。

You might want to take a look at the strstr function (if you're not already familiar with it).

墨离汐 2024-12-14 10:23:40
int main()
  {
          char *str = "This is demo";
          char *sub = "is";
          int i,j,count;
          i=j=count=0;
          while(str[i]!='\0')
          {
           if (str[i] == sub[j] && str[i+1] == sub[j+1])
          {
                  count++;
          }
          i++;
          }
          cout<<count;
          return 0;
  }

上面的代码可以工作,但这是静态的。

int main()
  {
          char *str = "This is demo";
          char *sub = "is";
          int i,j,count;
          i=j=count=0;
          while(str[i]!='\0')
          {
           if (str[i] == sub[j] && str[i+1] == sub[j+1])
          {
                  count++;
          }
          i++;
          }
          cout<<count;
          return 0;
  }

Above code works but this is static.

中二柚 2024-12-14 10:23:40

您可以在QT库中使用QString

QString t = "yourstring";
t.count("yoursubstring");

You can use QString in QT library

QString t = "yourstring";
t.count("yoursubstring");
回忆那么伤 2024-12-14 10:23:39

怎么样:(使用 strstr 函数,参考这里< /a>)

int count = 0;
char str1[50], str2[50];
char* tmp = str1;
int count;

printf("String 1:\n");
gets(str2);
printf("String 2:\n");
gets(str1);

while(*tmp != '\0' && (tmp = strstr(tmp, str2))) {
    ++count;
    ++tmp;
}

how about this: (using the strstr function, reference here)

int count = 0;
char str1[50], str2[50];
char* tmp = str1;
int count;

printf("String 1:\n");
gets(str2);
printf("String 2:\n");
gets(str1);

while(*tmp != '\0' && (tmp = strstr(tmp, str2))) {
    ++count;
    ++tmp;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文