英文:
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("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] == '.' || '!' || '?')
{
sentences++;
}
}
return sentences;
}
Its giving me:
readability.c:66:28: error: use of logical '||' with constant operand [-Werror,-Wconstant-
logical-operand]
if (text[i] == '.' || '!' || '?')
^ ~~~
readability.c:66:28: note: use '|' for a bitwise operation
if (text[i] == '.' || '!' || '?')
^~
|
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] == '.'
会评估为 true
或 false
,而 !
和 ?
两者都会评估为 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] == '.' || '!' || '?')
has the same meaning as
if (true)
That's because text[i] == '.'
evaluates to either true
or false
, but both '!'
and '?'
evaluate to true
, since their numeric value is not zero.
The correct test would be
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
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, ".!?"); // search for any of '.', '!', '?'
if (text == NULL) break; // if none of the chars was found
++sentences;
}
return sentences;
}
答案2
得分: 2
||
运算符比较两个布尔值,并在其中一个为 true 时停止评估。
在你的情况下,text[i] == '.' || '!' || '?'
这意味着:
- 评估
text[i] == '.'
。如果为 true,则停止并返回 true。否则, - 评估
'!'
。这是一个常量字符。因为它不等于 0,所以始终为 true。这不是你想要表达的逻辑,编译器检测到测试常量变量是奇怪的。这是一个警告,因为程序可以编译通过。很好地启用了-Werror
以便早期检测到这个问题!
你想要做的是继续将 text[i]
与其他常量进行比较,如下所示:
text[i] == '.' || text[i] == '!' || text[i] == '?'
。
由于这变得相当冗长,你可能希望稍后添加其他特殊字符,因此你会想使用更简洁的语法。请参考这个问题以获取更多信息: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] == '.' || '!' || '?'
, this means:
- evaluate
text[i] == '.'
. If true, stop and return true. Otherwise, - evaluate
'!'
. 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] == '.' || text[i] == '!' || text[i] == '?'
.
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论