Онлайн компилятор C

#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char word[1001]; int count = 0; // 使用 scanf("%1000s") 循环读取每一个单词,它会自动跳过所有空格、换行和制表符 while (scanf("%1000s", word) == 1) { int len = strlen(word); // 确保单词不为空(虽然 scanf 读取的单词长度至少为 1) if (len > 0) { // 将首尾字母转换为小写进行比较 char first_letter = tolower(word[0]); char last_letter = tolower(word[len - 1]); // 如果首尾字母相同,计数加 1 if (first_letter == last_letter) { count++; } } } // 输出最终满足条件的单词数量 printf("%d\n", count); return 0; }
This code reads words from input, converts the first and last letters to lowercase, and counts how many words have the same first and last letter. It uses scanf to read each word, skipping whitespace automatically.

- The code does not handle punctuation attached to words. For example, "hello!" would have last letter '!' instead of 'o', so it would not be counted even if the word is "hello".
- The code does not consider empty input or words with only one letter. A single-letter word like "a" would have first and last letter the same, which is correct, but you should verify if that is intended.