[英]Counting words using c
我想知道为什么我的代码没有正确编译。 我想要完成的是数单词。 我已经有了它要求空格的地方,但是如果它是一个字符串中的多个句子,那将不起作用。 因为当标点符号做同样的事情时,它会将标点符号后面的空格算作一个单词。
while (s[n] != '\0')
{
if (isalpha(s[n])) //counts letters
{
count++;
}
if (isspace(s[n])) //count words
{
word++;
}
if (s[n] == '.' || s[n] == '?' || s[n] == '!')
{
else if (s[n + 1] isspace) //This is the problem
{
word--;
}
sent++;
word++;
}
n++;
}
我很确定这是正确的,但它没有正确编译,所以我有点卡在这里。 根据我的理解, (s[n + 1]) 是说它当前正在检查的字母之后的字母是否是空格,然后查看它是否是空格,如果为真,则为单词 - 否则为单词++。 如果这是错误的,请告诉我为什么。
此代码可以无错误地编译:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char s[10] = "";
int n = 0;
int count = 0;
int word = 0;
int sent = 0;
while (s[n] != '\0')
{
if (isalpha(s[n])) //counts letters
{
count++;
}
if (isspace(s[n])) //count words
{
word++;
}
if (s[n] == '.' || s[n] == '?' || s[n] == '!')
{
// What to do here ?
}
else if (isspace(s[n + 1]))
{
word--;
}
sent++;
word++;
}
n++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.