问题
I want to find the number of characters inside a double quotation mark.
For example :
Case 1
"Hello World" , "Some
output : Error // missing quotation marks after Some
Case 2
"Hello Word" , "Some"
output : 14 // All quotation marks are complete
I have written the program for counting the total characters, the index of the first quotation mark and the total number of quotations marks using recursion.
What approach shall I use to solve this problem?
回答1:
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());
}
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/60977318/counting-the-number-of-characters-inside-double-quotation-marks