提取c中字符串的出现次数

发布于 2024-08-27 06:29:27 字数 462 浏览 8 评论 0原文

我有一个看起来像这样的字符串:

long_str = "returns between paragraphs 20102/34.23\" - 9203 1232 \"test\" \"basic HTML\"";

注意:引号是字符串的一部分。

int match(char *long_str){
    char * str;
    if ((str = strchr(long_str, '"')) != NULL) str++; // last " ?
    else return 1;
    return 0;
}

使用 strstr 我试图获取最后两个引号之间的整个子字符串:"basic HTML"。我只是不太确定什么是获得这场比赛的良好且有效的方式。我对如何解决这个问题的任何其他想法持开放态度。谢谢

I have a string that look something like this:

long_str = "returns between paragraphs 20102/34.23\" - 9203 1232 \"test\" \"basic HTML\"";

Note: Quotes are part of the string.

int match(char *long_str){
    char * str;
    if ((str = strchr(long_str, '"')) != NULL) str++; // last " ?
    else return 1;
    return 0;
}

Using strstr I'm trying to get the whole substring between the last two quotes: "basic HTML". I'm just not quite sure what would be a good and efficient way of getting that match. I'm open to any other ideas on how to approach this. Thanks

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

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

发布评论

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

评论(2

攀登最高峰 2024-09-03 06:29:27
#include <stdio.h>

char * long_str = 
"returns between paragraphs 20102/34.23\" - 9203 1232 \"test\" \"basic HTML\"";

int main ()
{
    char * probe;
    char * first_quote = 0;
    char * second_quote = 0;
    for (probe = long_str; * probe; probe++) {
        if (*probe == '"') {
            if (first_quote) {
                if (second_quote) {
                    first_quote = second_quote;
                    second_quote = probe;
                } else {
                    second_quote = probe;
                }
            } else {
                first_quote = probe;
            }
        }
    }
    printf ("%s\n", first_quote);
    printf ("%d-%d\n", first_quote - long_str, second_quote - long_str);
    return 0;
}
#include <stdio.h>

char * long_str = 
"returns between paragraphs 20102/34.23\" - 9203 1232 \"test\" \"basic HTML\"";

int main ()
{
    char * probe;
    char * first_quote = 0;
    char * second_quote = 0;
    for (probe = long_str; * probe; probe++) {
        if (*probe == '"') {
            if (first_quote) {
                if (second_quote) {
                    first_quote = second_quote;
                    second_quote = probe;
                } else {
                    second_quote = probe;
                }
            } else {
                first_quote = probe;
            }
        }
    }
    printf ("%s\n", first_quote);
    printf ("%d-%d\n", first_quote - long_str, second_quote - long_str);
    return 0;
}
浮华 2024-09-03 06:29:27

假设您无法修改源字符串,那么我认为您应该考虑 strchr()strrchr() 的组合 - 至少,如果您想的话一切都使用库函数。

  • 首先使用 strrchr() 查找最后一个引用。
  • 然后重复使用 strchr() 从前面查找引号,直到返回 strrchr() 找到的最后一个引号。
  • 前一个结果和 strrchr() 结果的组合为您提供了您感兴趣的字符串周围的引号。

或者:

  • 首先使用 strrchr() 查找最后一个引用。
  • 编写一个循环,从那里向后搜索以找到上一个引号,记住不要在字符串开头之前搜索,以免字符串格式错误且仅包含一个引号。

两者都会起作用。根据概率的平衡,循环的性能很可能甚至优于循环中包含的高度优化的 strchr()

Assuming that you can't modify the source string, then I think you should look at a combination of strchr() and strrchr() - at least, if you want to use library functions for everything.

  • First use strrchr() to find the last quote.
  • Then repeatedly use strchr() to find quotes from the front, up to the time it returns the last quote found by strrchr().
  • The combination of the previous result and the result of strrchr() gives you the quotes around the string you are interested in.

Alternatively:

  • First use strrchr() to find the last quote.
  • Write a loop to search backwards from there to find the previous quote, remembering not to search before the start of the string in the case that it is malformed and contains only one quote.

Both will work. Depending on the balance of probabilities, it is quite possible that the loop will outperform even a highly optimized strchr() wrapped in a loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文