Counting the number of characters inside double quotation marks

前端 未结 3 1990
失恋的感觉
失恋的感觉 2021-01-28 01:23

I want to find the number of characters inside a double quotation mark.

For example :

Case 1

\"Hello World\" , \"Some

output :

相关标签:
3条回答
  • 2021-01-28 01:25

    You could use something simple like finding the positions of the double quotes and subtracting them.

    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;
    

    There are issues with the above code, such as checking if the double quotes exist, that the OP should implement.

    This is one of many possible implementations.

    0 讨论(0)
  • 2021-01-28 01:35

    Here is a working version using recursion. For readability I split it in two functions that call each other recursively.

    I'm using a pointer to the character after the last quotation mark to keep track of what to print if the number of marks don't match, and at the same time initializing it to nullptr so it also keeps track of if we have encountered any quotation marks at all.

    #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());
    }
    
    0 讨论(0)
  • 2021-01-28 01:41

    Please help me to figure out what approach shall I use to solve the above-given problem.

    Instead of using recursion, you may use this simpler approach that runs in linear time:

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

    Edit:

    If you need a better explanation, it is in JohnFilleau's comment below the question.

    0 讨论(0)
提交回复
热议问题