CS50 问题 2 - 可读性 // 评分工作不正常

CS50 problem 2 - Readability // The grading is not working properly

您好,我遇到了 CS50 问题集 2 的问题,特别是可读性任务。问题是当我输入网站上的预设值以尝试测试您的程序时,其中一些不起作用我不确定为什么它不起作用任何人都可以帮助我

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

int main(void){
  string text = get_string("Text: ");
  int letter_count = 0;
  int word_count = 0;
  int sentence_count = 0;

  for( int i=0;i < strlen(text);i++){

    if (isalpha(text[i])){
        letter_count ++;
    }
    else if(text[i] == ' '){
        word_count ++;
    }
    else if(text[i] == '.' || text[i] == '!' || text[i] == '?')
    {
        sentence_count ++;
    }
  }
  float L = ((float)letter_count / (float)word_count) * 100;
  float S = ((float)sentence_count / (float)word_count) * 100;
  float index = 0.0588 * L - 0.296 * S - 15.8;

  if (index <= 16 && index >= 0)
  {
    printf("Grade %i\n", (int) round(index));
  }
  else if (index >= 16)
  {
    printf("Grade 16+\n");
  }
  else{
    printf("Before Grade 1\n");
  }
  printf("%i Letter(s)\n", letter_count);
  printf("%i Word(s)\n", word_count+1);
  printf("%i Sentence(s)\n", sentence_count);
}

当您检查 spaces 来计算单词时,一个 space 意味着 ' ' 确实是一个单词。但是如果有一个space,就意味着前面一定要有一个词。像下面一样;

Hello World.

这里有两个字,但是有一个space。要解决这个问题,用 1 初始化 word 变量应该可以解决这个问题。

int word_count = 1;