在线编译器 C

#include <ctype.h> #include <stdio.h> #include <string.h> int main() { char word[1001]; int count = 0; while (scanf("%1000s", word) == 1) { int len = strlen(word); if (len > 0) { char first_letter = tolower(word[0]); char last_letter = tolower(word[len - 1]); if (first_letter == last_letter) { count++; } } } printf("%d\n", count); return 0; }
这段代码的功能是:从标准输入读取多个单词(以空白字符分隔),统计那些首字母和尾字母(忽略大小写)相同的单词个数,最后输出统计结果。

代码逻辑基本正确,但有一个潜在问题需要注意:

- 当读取的单词中包含标点符号时,比如 "hello!",程序会把感叹号当作单词的最后一个字符,导致首尾字母比较时出错。题目可能要求只考虑字母字符,或者需要过滤掉非字母字符。

- 另外,`scanf` 使用 `%s` 格式会跳过空白字符,但不会处理单词内部的标点,这可能导致统计结果与预期不符。