如何计算段落中的单词数?我一直数错
How may I count the number of words in a paragraph? I keep miscounting
我正在尝试编写一个程序来计算文本中字母、单词和句子的数量。我可以假设一个字母是从 a 到 z 的任何小写字符或从 A 到 Z 的任何大写字符,任何由空格分隔的字符序列都应该算作一个单词,并且任何出现的句点、感叹号或问号表示句子的结束。
到目前为止,我可以正确数出字母和句子的数量,但我错过了单词数:
例如
是的!
输出应该是:
3 个字母
1个字
1 句
我得到的是:
3 个字母
0 字
1 句
更新:在 printf 函数之前最后输入另一个 (words++) 后,它现在工作正常。感谢大家的帮助:).
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
string text = get_string("Enter text: ");
printf("Output:\n");
int lettercount;
int words = 0;
int sentences = 0;
int letters = 0;
int length = strlen(text);
for(lettercount = 0; lettercount < length; lettercount++)
{
if(isalpha(text[lettercount]))
{
letters++;
}
else if(text[lettercount] == ' ' || text[lettercount] == '\t' || text[lettercount] == '\n' || text[lettercount] == '[=10=]')
{
words++;
}
else if(text[lettercount] == '.' || text[lettercount] == '!' || text[lettercount] == '?')
{
sentences++;
}
}
words++;
printf("%i letter(s)\n", letters);
printf("%i word(s)\n", words);
printf("%i sentence(s)\n", sentences);
}
你总是有单词 -1 因为你只在 space 或新行之后向你的计数器添加新单词但是最后一个单词呢!?总是最后一个单词不会被计算在内,所以在计算任何段落后将 1 添加到您的单词计数器..
例如:是的! --> 3 个字母 1 个句子 0 个单词!所以你加一个就解决了
另一个例子:你好世界! --> 10 个字母 1 个句子 1 个单词!加一个就解决了
您的代码的主要问题是,如果输入文本后没有 space(终止 '[=11=]'
字符),它不会计算输入文本中的任何 'final' 字不会成为测试字符串的一部分,因为strlen
函数不包含它。
此外,如果您的单词被多个单词分隔,您将遇到问题 space;为了解决这个问题,您可以使用 inWord
标志来跟踪当前字符是否已经在单词中,如果不在,则在我们找到字母时设置该标志。
此外,如果您的输入中有 "..."
之类的内容,您的句子计数也会有问题; sentences++;
行后的注释行将解决该问题(如果需要)。
最后,为了准确起见,您不应假定字母“a”到“z”和“A”到“Z”是连续的。它们 可能 会(如今大多数系统都使用 ASCII 编码)但是您应该使用 isalpha
函数以获得更好的可移植性(以及 isspace
函数)。
int main(void)
{
string text = get_string("Enter text: ");
printf("Output:\n");
int lettercount;
int words = 0;
int sentences = 0;
int letters = 0;
int inWord = 0;// Set to 1 if we are inside a (new) word!
int length = (int)(strlen(text)); // Don't evaluate length on each loop!
for (lettercount = 0; lettercount < length; lettercount++) {
int testChar = text[lettercount]; // Get a local copy of the current character
if (isalpha(testChar)) { // Don't assume that 'a' ... 'z' and 'A' ... 'Z' are in contiguous sequences
letters++;
if (!inWord) words++; // Any letter means that we're in a (possibly new) word...
inWord = 1; // ... but now set this 'flag' so as not to count others!
}
else if (testChar == '.' || testChar == '!' || testChar == '?') {
sentences++;
// if (inWord) sentences++; // Check that we're in a word, or stuff like "..." will be wrong
inWord = 0; // Now we are no longer inside our current word
}
else if (isspace(testChar)) { // We could also just assume ANY other character is a non-word
inWord = 0; // Now we are no longer inside our current word
}
}
printf("%i letter(s)\n", letters);
printf("%i word(s)\n", words);
printf("%i sentence(s)\n", sentences);
return 0;
}
如有任何进一步的说明,请随时提出 and/or 解释。
我正在尝试编写一个程序来计算文本中字母、单词和句子的数量。我可以假设一个字母是从 a 到 z 的任何小写字符或从 A 到 Z 的任何大写字符,任何由空格分隔的字符序列都应该算作一个单词,并且任何出现的句点、感叹号或问号表示句子的结束。
到目前为止,我可以正确数出字母和句子的数量,但我错过了单词数:
例如 是的!
输出应该是: 3 个字母 1个字 1 句
我得到的是: 3 个字母 0 字 1 句
更新:在 printf 函数之前最后输入另一个 (words++) 后,它现在工作正常。感谢大家的帮助:).
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
string text = get_string("Enter text: ");
printf("Output:\n");
int lettercount;
int words = 0;
int sentences = 0;
int letters = 0;
int length = strlen(text);
for(lettercount = 0; lettercount < length; lettercount++)
{
if(isalpha(text[lettercount]))
{
letters++;
}
else if(text[lettercount] == ' ' || text[lettercount] == '\t' || text[lettercount] == '\n' || text[lettercount] == '[=10=]')
{
words++;
}
else if(text[lettercount] == '.' || text[lettercount] == '!' || text[lettercount] == '?')
{
sentences++;
}
}
words++;
printf("%i letter(s)\n", letters);
printf("%i word(s)\n", words);
printf("%i sentence(s)\n", sentences);
}
你总是有单词 -1 因为你只在 space 或新行之后向你的计数器添加新单词但是最后一个单词呢!?总是最后一个单词不会被计算在内,所以在计算任何段落后将 1 添加到您的单词计数器.. 例如:是的! --> 3 个字母 1 个句子 0 个单词!所以你加一个就解决了 另一个例子:你好世界! --> 10 个字母 1 个句子 1 个单词!加一个就解决了
您的代码的主要问题是,如果输入文本后没有 space(终止 '[=11=]'
字符),它不会计算输入文本中的任何 'final' 字不会成为测试字符串的一部分,因为strlen
函数不包含它。
此外,如果您的单词被多个单词分隔,您将遇到问题 space;为了解决这个问题,您可以使用 inWord
标志来跟踪当前字符是否已经在单词中,如果不在,则在我们找到字母时设置该标志。
此外,如果您的输入中有 "..."
之类的内容,您的句子计数也会有问题; sentences++;
行后的注释行将解决该问题(如果需要)。
最后,为了准确起见,您不应假定字母“a”到“z”和“A”到“Z”是连续的。它们 可能 会(如今大多数系统都使用 ASCII 编码)但是您应该使用 isalpha
函数以获得更好的可移植性(以及 isspace
函数)。
int main(void)
{
string text = get_string("Enter text: ");
printf("Output:\n");
int lettercount;
int words = 0;
int sentences = 0;
int letters = 0;
int inWord = 0;// Set to 1 if we are inside a (new) word!
int length = (int)(strlen(text)); // Don't evaluate length on each loop!
for (lettercount = 0; lettercount < length; lettercount++) {
int testChar = text[lettercount]; // Get a local copy of the current character
if (isalpha(testChar)) { // Don't assume that 'a' ... 'z' and 'A' ... 'Z' are in contiguous sequences
letters++;
if (!inWord) words++; // Any letter means that we're in a (possibly new) word...
inWord = 1; // ... but now set this 'flag' so as not to count others!
}
else if (testChar == '.' || testChar == '!' || testChar == '?') {
sentences++;
// if (inWord) sentences++; // Check that we're in a word, or stuff like "..." will be wrong
inWord = 0; // Now we are no longer inside our current word
}
else if (isspace(testChar)) { // We could also just assume ANY other character is a non-word
inWord = 0; // Now we are no longer inside our current word
}
}
printf("%i letter(s)\n", letters);
printf("%i word(s)\n", words);
printf("%i sentence(s)\n", sentences);
return 0;
}
如有任何进一步的说明,请随时提出 and/or 解释。