【问题标题】:Counting words that start with Capital Letters C++ [Help]计算以大写字母 C++ 开头的单词 [帮助]
【发布时间】:2022-01-08 03:00:35
【问题描述】:

大家好,我是 C++ 的新手,需要一些帮助。 我正在尝试编写一个仅计算以大写字母开头的单词的程序。

int countLegalWords(char str[])

int counter = 0; // counts the legal words

for (int i = 0; i < MAXSIZE; i++)
{
    if (str[i] >= 'A' && str[i] <= 'Z')
    {
        if (str[i + 1] >= 'a' && str[i + 1] <= 'z')
        {
            counter++;
        }
        else if (str[i] == ' ')
            i++;
    }
}

return counter;

示例

输入:

喜欢 ce Cream H

输出:

4 个单词,单词开头带有大写字母。

【问题讨论】:

  • 那么这段代码除了缺少{}还有什么问题
  • 我不喜欢MAXSIZE。你怎么知道str[] 包含那么多字符?
  • 无论您使用什么资源来学习 C++,我都认为它做得不是很好。首先,您应该几乎总是使用std::string 来表示字符串。其次,您对大小写字母的检查有缺陷,您应该改用std::isupperstd::islower
  • str[i + 1] 可能是一个错误。而if (str[i] == ' ') 永远不会是真的,因为你知道str[i] 介于'A' 和'Z' 之间,因为它进入了第一个if () 的上述块
  • 您的代码也存在一些逻辑问题。使用 debugger 逐句执行代码,同时监控变量及其值,以帮助您找出问题所在。

标签: c++ visual-c++


【解决方案1】:

你的代码有几个问题

  1. 在 C++ 中,std::string 用于字符串。 'char*' 或 'char[]' 之类的东西在 C++ 中不使用它们在 C 中使用。但是 C 和 C++ 是两种不同的语言
  2. 您使用 C 样式数组 (char str[])。在 C++ 中,不使用 C 样式数组
  3. 有一个神奇的常数MAXSIZE,它与字符串的长度无关。如果 MAXSIZE 为 100 但您的字符串较小,则您在 for 循环中比较超出范围的值。这是一个严重的错误。
  4. 您需要了解 C 风格的字符串是以 0 结尾的。最后一个字符是'\0'。您不得在终止 0 之后操作。
  5. 您检测单词和单词开头的逻辑是错误的。您检查 any 字符是否为大写,然后检查下一个字符是否为小写。但这也可能发生在一个词之内。看看你的句子里有“喜欢”。
  6. 您不想切换到 C++。你在评论中写了你想使用strcmp之类的,这是一个C函数
  7. 如果您会编写 cmets 并使用有意义的变量名,那么您会自己发现所有错误。

好的,这就是问题所在。现在是一个可能的解决方案,使用您的编程风格。

#include <iostream>

int countLegalWords(char myString[]) {

    // Here we will store the number of words which start with a capital letter
    int countOfLegalWords = 0; 

    // We will iterate over the complete string and check all characters
    unsigned int indexInString = 0;

    // Check all characters of the string until we hit the terminating 0
    while (myString[indexInString] != '\0') {

        // Search for the begin of a word. This is the first non-space character
        while (myString[indexInString] == ' ')
            ++indexInString;

        // OK, Now we found the begin of a word. Check, if the first character is upper case
        if ((myString[indexInString] >= 'A') and (myString[indexInString] <= 'Z')) {

            // Yes, uppercase, count up
            ++countOfLegalWords;
        }
        // Now search for the end of the word or for the end of the complete string
        while ((myString[indexInString] != '\0') and (myString[indexInString] != ' '))
            ++indexInString;

    }
    // And return the count of legal words to ther calling program
    return countOfLegalWords;
}

int main() {
    char myString[] = "I liKE Ice Cream H";
    std::cout << "\nCount of legal words: " << countLegalWords(myString) << '\n';
}

在 C++ 中,所有这些都将通过一个衬里完成:

#include <iostream>
#include <string>
#include <iterator>
#include <regex>

// Regex for a word starting with a capital letter
const std::regex re{ R"(\b[A-Z]\w*)" };

// Main program
int main() {
    std::string myString{ "I liKE Ice Cream H" };
    std::cout << "\nCount of legal words: " << std::distance(std::sregex_token_iterator(myString.begin(), myString.end(),re), {}) << '\n';
}

【讨论】:

  • 非常感谢 :) !
猜你喜欢
  • 1970-01-01
  • 2018-09-03
  • 2020-07-28
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 2021-12-18
相关资源
最近更新 更多