【问题标题】:Counting the number of characters inside double quotation marks计算双引号内的字符数
【发布时间】:2020-04-01 17:58:04
【问题描述】:

我想查找双引号内的字符数。

例如:

案例 1

“你好世界”,“一些

输出:错误 // Some 后面缺少引号

案例 2

“Hello Word”、“一些”

output : 14 // 所有引号都完整

我已经编写了使用递归计算总字符数、第一个引号索引和引号总数的程序。

我应该用什么方法来解决这个问题?

【问题讨论】:

  • 不要使用递归。
  • 如果你必须使用递归,那么编写两个相互递归的例程,一个用于处理任何引号之外,一个用于处理引号内。当任一例程找到引用时,它会调用另一个例程。
  • 一次性遍历字符串有什么问题,每次点击引号时都会切换计数功能?

标签: c++ algorithm recursion


【解决方案1】:

请帮我弄清楚我应该用什么方法来解决这个问题 上面给出的问题。

您可以使用这种在线性时间中运行的更简单的方法,而不是使用递归:

void countCharsWithinDoubleQuotes(const std::string& input)
{
    size_t ans = 0;
    bool quoteStarted = false;

    // Iterate through the string and get the required counts
    for (const auto& ch : input)
    {
        // Toggle the quote switch
        if (ch == '"')
            quoteStarted = !quoteStarted;

        // Keep counting the characters after a starting double quote
        else if (quoteStarted)
            ++ans;
    }

    // If a closing quote was not found, it was not balanced
    if (quoteStarted) // quoteDidNotEnd
    {
        std::cout << "Error";
    }

    // Print the number of characters within all the double quotes
    else
    {
        std::cout << ans;
    }
}

编辑:

如果您需要更好的解释,请在问题下方的 JohnFilleau 评论中找到。

【讨论】:

  • @xskxzr 很好。我会用你的建议更新我的答案:)
【解决方案2】:

这是一个使用递归的工作版本。为了便于阅读,我将其拆分为两个递归调用的函数。

我使用指向最后一个引号后的字符的指针来跟踪如果标记的数量不匹配要打印什么,同时将其初始化为nullptr,因此它也可以跟踪如果我们遇到任何引号。

#include <iostream>

int count_marks(const char* str, int count = 0, const char* err = nullptr);
int count_between_marks(const char* str, int count = 0, const char* err = nullptr);

int count_marks(const char* str, int count, const char* err) {
    if (!*str) {
        if (!err) {
            std::cout << "Error // No quotation marks at all\n";
            return -1;
        }

        std::cout << count << " // All quotation marks are complete\n";
        return count;
    }

    if (*str == '\"')
        return count_between_marks(str+1, count, str+1);
    else
        return count_marks(str+1, count, err);
}

int count_between_marks(const char* str, int count, const char* err) {
    if (!*str) {
        std::cout << "Error // No quotation marks after " << err << '\n';
        return -1;
    }
    if (*str == '\"')
        return count_marks(str+1, count, err);
    else
        return count_between_marks(str+1, count+1, err);
}

int main() {
    std::string s("\"Hello World\", \"Some\"");

    std::string s2("\"Hello World\", \"Some");

    std::string s3("Hello World, Some");

    count_marks(s.c_str());
    count_marks(s2.c_str());
    count_marks(s3.c_str());
}

【讨论】:

    【解决方案3】:

    您可以使用一些简单的方法,例如找到双引号的位置并减去它们。

    static const std::string example = "The \"comment\" is quoted.";
    const std::string::size_type first_position = example.find('"');
    const std::string::size_type second_position = example.find('"', second_position + 1);
    const unsigned int character_quantity = second_position - first_position;
    

    上述代码存在问题,例如检查双引号是否存在,OP 应该实现。

    这是许多可能的实现之一。

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      相关资源
      最近更新 更多