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

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

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

问题

以下是代码部分的翻译:

  1. 这是我的代码:导致错误的函数是 `count_sentences`
  2. int count_letters(string text);
  3. int count_words(string text);
  4. int count_sentences(string text);
  5. int main(void)
  6. {
  7. string text = get_string("Text: ");
  8. printf("%s\n", text);
  9. int letters = count_letters(text);
  10. printf("%i letters\n", letters);
  11. int words = count_words(text);
  12. printf("%i words\n", words);
  13. int sentences = count_sentences(text);
  14. printf("%i sentences\n", sentences);
  15. }
  16. int count_letters(string text)
  17. {
  18. int letters = 0;
  19. for (int i = 0, len = strlen(text); i < len; i++)
  20. {
  21. if (isalpha(text[i]))
  22. {
  23. letters++;
  24. }
  25. }
  26. return letters;
  27. }
  28. int count_words(string text)
  29. {
  30. int words = 0;
  31. for (int i = 0, len = strlen(text); i < len; i++)
  32. {
  33. if (text[i] == ' ')
  34. {
  35. words++;
  36. }
  37. }
  38. return words + 1;
  39. }
  40. int count_sentences(string text)
  41. {
  42. int sentences = 0;
  43. for (int i = 0, len = strlen(text); i < len; i++)
  44. {
  45. if (text[i] == '.' || text[i] == '!' || text[i] == '?')
  46. {
  47. sentences++;
  48. }
  49. }
  50. return sentences;
  51. }
  52. 它给我报错:
  53. readability.c:66:28: 错误:使用带有常量操作数的逻辑‘|| [-Werror-Wconstant-logical-operand]
  54. if (text[i] == '.' || '!' || '?')
  55. ^ ~~~
  56. readability.c:66:28: 注意:用‘|’进行位运算
  57. if (text[i] == '.' || '!' || '?')
  58. ^~
  59. |
  60. 致命错误:错误数过多,现在停止 [-ferror-limit=]
  61. 有人知道如何修复吗?我知道我可以只需创建一个`else if`块并获得相同的结果,但我想知道是否可以这样做。它似乎更有效,并且看起来更好看。
  62. stackoverflow给我一个主要是代码的错误,所以请忽略这个。
英文:

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

  1. int count_letters(string text);
  2. int count_words(string text);
  3. int count_sentences(string text);
  4. int main(void)
  5. {
  6. string text = get_string(&quot;Text: &quot;);
  7. printf(&quot;%s\n&quot;, text);
  8. int letters = count_letters(text);
  9. printf(&quot;%i letters\n&quot;, letters);
  10. int words = count_words(text);
  11. printf(&quot;%i words\n&quot;, words);
  12. int sentences = count_sentences(text);
  13. printf(&quot;%i sentences\n&quot;, sentences);
  14. }
  15. int count_letters(string text)
  16. {
  17. int letters = 0;
  18. for (int i = 0, len = strlen(text); i &lt; len; i++)
  19. {
  20. if (isalpha(text[i]))
  21. {
  22. letters++;
  23. }
  24. }
  25. return letters;
  26. }
  27. int count_words(string text)
  28. {
  29. int words = 0;
  30. for (int i = 0, len = strlen(text); i &lt; len; i++)
  31. {
  32. if (text[i] == &#39; &#39;)
  33. {
  34. words++;
  35. }
  36. }
  37. return words + 1;
  38. }
  39. int count_sentences(string text)
  40. {
  41. int sentences = 0;
  42. for (int i = 0, len = strlen(text); i &lt; len; i++)
  43. {
  44. if (text[i] == &#39;.&#39; || &#39;!&#39; || &#39;?&#39;)
  45. {
  46. sentences++;
  47. }
  48. }
  49. return sentences;
  50. }

Its giving me:

  1. readability.c:66:28: error: use of logical &#39;||&#39; with constant operand [-Werror,-Wconstant-
  2. logical-operand]
  3. if (text[i] == &#39;.&#39; || &#39;!&#39; || &#39;?&#39;)
  4. ^ ~~~
  5. readability.c:66:28: note: use &#39;|&#39; for a bitwise operation
  6. if (text[i] == &#39;.&#39; || &#39;!&#39; || &#39;?&#39;)
  7. ^~
  8. |
  9. 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

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

  1. if (true)

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

正确的测试应该是

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

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

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

演示

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

has the same meaning as

  1. 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

  1. 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:

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

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:

确定