尝试创建一个if语句,检查文本中是否包含., !或?,但它一直报错。

huangapple go评论181阅读模式
英文:

Trying to make an if statement that checks if a text has ., ! or ? in it but it keeps giving errors

问题

以下是代码部分的翻译:

这是我的代码:导致错误的函数是 `count_sentences`。

int count_letters(string text);
int count_words(string text);
int count_sentences(string text);

int main(void)
{
    string text = get_string("Text: ");
    printf("%s\n", text);

    int letters = count_letters(text);
    printf("%i letters\n", letters);

    int words = count_words(text);
    printf("%i words\n", words);

    int sentences = count_sentences(text);
    printf("%i sentences\n", sentences);
}

int count_letters(string text)
{
    int letters = 0;

    for (int i = 0, len = strlen(text); i < len; i++)
    {
        if (isalpha(text[i]))
        {
            letters++;
        }
    }
    return letters;
}

int count_words(string text)
{
    int words = 0;

    for (int i = 0, len = strlen(text); i < len; i++)
    {
        if (text[i] == ' ')
        {
            words++;
        }
    }
    return words + 1;
}

int count_sentences(string text)
{
    int sentences = 0;

    for (int i = 0, len = strlen(text); i < len; i++)
    {
        if (text[i] == '.' || text[i] == '!' || text[i] == '?')
        {
            sentences++;
        }
    }
    return sentences;
}

它给我报错:

readability.c:66:28: 错误:使用带有常量操作数的逻辑‘|| [-Werror-Wconstant-logical-operand]
        if (text[i] == '.' || '!' || '?')
                           ^  ~~~
readability.c:66:28: 注意:用‘|’进行位运算
        if (text[i] == '.' || '!' || '?')
                           ^~
                           |
致命错误:错误数过多,现在停止 [-ferror-limit=]

有人知道如何修复吗?我知道我可以只需创建一个`else if`块并获得相同的结果,但我想知道是否可以这样做。它似乎更有效,并且看起来更好看。

stackoverflow给我一个主要是代码的错误,所以请忽略这个。
英文:

Here is my code: the function that gives me errors is count_sentences.

int count_letters(string text);
int count_words(string text);
int count_sentences(string text);
int main(void)
{
string text = get_string(&quot;Text: &quot;);
printf(&quot;%s\n&quot;, text);
int letters = count_letters(text);
printf(&quot;%i letters\n&quot;, letters);
int words = count_words(text);
printf(&quot;%i words\n&quot;, words);
int sentences = count_sentences(text);
printf(&quot;%i sentences\n&quot;, sentences);
}
int count_letters(string text)
{
int letters = 0;
for (int i = 0, len = strlen(text); i &lt; len; i++)
{
if (isalpha(text[i]))
{
letters++;
}
}
return letters;
}
int count_words(string text)
{
int words = 0;
for (int i = 0, len = strlen(text); i &lt; len; i++)
{
if (text[i] == &#39; &#39;)
{
words++;
}
}
return words + 1;
}
int count_sentences(string text)
{
int sentences = 0;
for (int i = 0, len = strlen(text); i &lt; len; i++)
{
if (text[i] == &#39;.&#39; || &#39;!&#39; || &#39;?&#39;)
{
sentences++;
}
}
return sentences;
}

Its giving me:

readability.c:66:28: error: use of logical &#39;||&#39; with constant operand [-Werror,-Wconstant- 
logical-operand]
if (text[i] == &#39;.&#39; || &#39;!&#39; || &#39;?&#39;)
^  ~~~
readability.c:66:28: note: use &#39;|&#39; for a bitwise operation
if (text[i] == &#39;.&#39; || &#39;!&#39; || &#39;?&#39;)
^~
|
fatal error: too many errors emitted, stopping now [-ferror-limit=]

Anyone know how to fix it? I know I can just make an else if block and get the same result but I would like to know if I could do it this way. it seems more effective and looks a lot nicer.

stackoverflow is giving me an error that is mostly code so ignore this please

答案1

得分: 3

if (text[i] == '.' || text[i] == '!' || text[i] == '?')

if (true)

具有相同的意思。这是因为 text[i] == '.' 会评估为 truefalse,而 !? 两者都会评估为 true,因为它们的数字值不为零。

正确的测试应该是

if (text[i] == '.' || text[i] == '!' || text[i] == '?')

或者更简单的方法是使用 strpbrk 来检查整个字符串,它会返回找到的字符的指针,如果没有找到要搜索的字符,则返回 NULL

unsigned count_sentences(const char* text) {
    unsigned sentences = 0;
    for (; *text; ++text) {
        text = strpbrk(text, ".!?"); // 搜索任何一个字符'.', '!', '?'
        if (text == NULL) break;     // 如果没有找到这些字符中的任何一个
        ++sentences;
    }
    return sentences;
}

演示


<details>
<summary>英文:</summary>
```c
if (text[i] == &#39;.&#39; || &#39;!&#39; || &#39;?&#39;)

has the same meaning as

if (true)

That's because text[i] == &#39;.&#39; evaluates to either true or false, but both &#39;!&#39; and &#39;?&#39; evaluate to true, since their numeric value is not zero.

The correct test would be

if (text[i] == &#39;.&#39; || text[i] == &#39;!&#39; || text[i] == &#39;?&#39;)

or simpler, check the whole string with strpbrk which returns a pointer to the found char or NULL if none of the characters to search for is found:

unsigned count_sentences(const char* text) {
    unsigned sentences = 0;
    for (; *text; ++text) {
        text = strpbrk(text, &quot;.!?&quot;); // search for any of &#39;.&#39;, &#39;!&#39;, &#39;?&#39;
        if (text == NULL) break;     // if none of the chars was found
        ++sentences;
    }
    return sentences;
}

Demo

答案2

得分: 2

|| 运算符比较两个布尔值,并在其中一个为 true 时停止评估。

在你的情况下,text[i] == &#39;.&#39; || &#39;!&#39; || &#39;?&#39; 这意味着:

  1. 评估 text[i] == &#39;.&#39;。如果为 true,则停止并返回 true。否则,
  2. 评估 &#39;!&#39;。这是一个常量字符。因为它不等于 0,所以始终为 true。这不是你想要表达的逻辑,编译器检测到测试常量变量是奇怪的。这是一个警告,因为程序可以编译通过。很好地启用了 -Werror 以便早期检测到这个问题!

你想要做的是继续将 text[i] 与其他常量进行比较,如下所示:
text[i] == &#39;.&#39; || text[i] == &#39;!&#39; || text[i] == &#39;?&#39;

由于这变得相当冗长,你可能希望稍后添加其他特殊字符,因此你会想使用更简洁的语法。请参考这个问题以获取更多信息:https://stackoverflow.com/questions/1071542/in-c-check-if-a-char-exists-in-a-char-array

英文:

The || operator compares two booleans and stops evaluating them as soon as one is true.
In your case, text[i] == &#39;.&#39; || &#39;!&#39; || &#39;?&#39;, this means:

  1. evaluate text[i] == &#39;.&#39;. If true, stop and return true. Otherwise,
  2. evaluate &#39;!&#39;. This is a constant character. Because it is not 0, it is always true. That's not the logic you want to express and the compiler detects that it is weird to test a constant variable. This is a warning because the program could compile like that. Good job on activating -Werror to detect this early!

What you want to do is keep comparing text[i] to other constants like that:
text[i] == &#39;.&#39; || text[i] == &#39;!&#39; || text[i] == &#39;?&#39;.

Since this is getting quite verbose and you might want to add other special characters later, you will want to use a more condensed syntax. Refer to this question for that: https://stackoverflow.com/questions/1071542/in-c-check-if-a-char-exists-in-a-char-array

huangapple
  • 本文由 发表于 2023年8月10日 19:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定