使用 c 计算单词
Counting words using c
我想知道为什么我的代码编译不正确。我想要完成的是计算单词。我已经找到它要求 spaces 的地方,但如果它是一个字符串中的多个句子,那将不起作用。因为当标点符号做同样的事情时,它会将标点符号后面的 space 算作一个单词。
while (s[n] != '[=10=]')
{
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]) 是说如果它当前正在检查的那个字母之后的字母是 space 然后看看它是否是 space,如果为真则单词 - 否则字++。如果这是错误的,请告诉我为什么。
这段代码可以编译而不出错:
#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] != '[=10=]')
{
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++;
}
我想知道为什么我的代码编译不正确。我想要完成的是计算单词。我已经找到它要求 spaces 的地方,但如果它是一个字符串中的多个句子,那将不起作用。因为当标点符号做同样的事情时,它会将标点符号后面的 space 算作一个单词。
while (s[n] != '[=10=]')
{
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]) 是说如果它当前正在检查的那个字母之后的字母是 space 然后看看它是否是 space,如果为真则单词 - 否则字++。如果这是错误的,请告诉我为什么。
这段代码可以编译而不出错:
#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] != '[=10=]')
{
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++;
}