【发布时间】:2020-09-27 20:26:03
【问题描述】:
我知道 strchr 只获得第一个实例。但是为什么我的循环不起作用。不管多少次'。要么 ' !'要么 '?'文中使用。一直输出3。
//this finds number of sentences
int Y = 0;
while (ch != '\0');
if(strchr(text,'.') != NULL)
Y++;
if(strchr(text,'!') != NULL)
Y++;
if(strchr(text,'?') != NULL)
Y++;
【问题讨论】:
-
while (ch != '\0');是无限循环。 (提示:看;) -
使用大括号。总是。几年后,如果您决定在某些情况下不使用大括号,那很好,但现在,总是
while (...) { if (...) { } } -
strchr不循环。它返回一个指向匹配的指针。如果您希望某些内容被递增并用作循环中的条件,您可能希望获取 strchr 返回的值并将其递增。
标签: c loops char c-strings strchr