字长计数器不将单引号计为一个字符

Word length counter isn't counting single quotation as a character

#include <string.h>
#include <stdio.h>
#include <ctype.h>
#define SIZE 100

const char delim[] = ", . - !*()&^%$#@<> ? []{}";

 int main(void)
{
int length[SIZE] = { 0 };
int name[SIZE];
int i = 0, ch;
int wordlength = 0;
int occurence = 0;

printf("Please enter sentence, end the setence with proper punctuation(! . ?)  : ");

while (1)
{
    ch = getchar();

    if (isalpha(ch))
    {
         ++wordlength;
    }
    else if (strchr(delim, ch))
    {
        if (wordlength)
        {
            length[wordlength - 1]++;
        }

        if(ch == '.' || ch == '?' || ch == '!')
        {
            break;
        }

        if(ch == '\'')
        {
            wordlength++;
        }

        wordlength = 0;
     }
 }

printf("Word Length \tOccurence \n");

for(i = 0; i<sizeof(length) / sizeof(*length); ++i)
{
    if(length[i])
    {
        printf(" %d \t\t%d\n", i + 1, length[i]);
    }
}

 return 0;
}

所以程序应该算取一个句子如...

 "Hello how are you?" 

然后输出:

  5 Letter Words -> 1
  3 Letter Words -> 3

但是我希望它也将单引号算作一个字符,例如...

输入:

  " 'Tis good. "

输出:

 4 Letter words -> 2

我试过使用...

  if(ch == '\'')
        {
            wordlength++;
        }

但它只是不把单引号算作一个字符。

您的 if (ch =='\'') { ... } 中缺少 continue;这就是为什么 wordlength 之后立即归零的原因。

另一个解决方案是在第一个 if 条件中包含 '

if (isalpha(ch) || ch == '\'') {
    wordlength ++;
}