Check if the given string is a valid string literal.

string checkString(string c)
{
    int num = c.size();
    if (num >= 2 && c.find_first_of("\"") == 0 && c.find_last_of("\"") == num - 1)
    {
        int count = 0;
        for (int i = 0; i < num; i++)
        {
            if (c.at(i) == '\"')
                count++;
            else if (c.at(i) == '\\')
                i++;
        }
        if (count != num && count % 2 == 0)
            return "true";
        return "false";
    }

    return "false";
}

你可能感兴趣的:(C/C++)