You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写统计大写字母并找出大写字母最多单词的C代码出错,寻求问题排查帮助

Hey there! Let's tackle your two requests one by one—first identifying words with uppercase letters, then fixing that buggy code for counting max uppercase per word.

需求1:找出包含大写字母的单词并按序号输出

To pull this off, we need to split your input into words, track their order, and check each one for uppercase letters. Here's a straightforward implementation in C that does exactly that:

#include <stdio.h>
#include <ctype.h>

#define IN 1  // Inside a word
#define OUT 0 // Outside a word

int main() {
    int c, state = OUT;
    int word_number = 0;
    char current_word[100]; // Adjust size if you need longer words
    int char_index = 0;
    int has_uppercase = 0;

    while ((c = getchar()) != EOF) {
        if (isspace(c)) {
            if (state == IN) {
                // End of a word—wrap it up and check for uppercase
                current_word[char_index] = '\0';
                char_index = 0;
                state = OUT;
                word_number++;
                
                if (has_uppercase) {
                    printf("序号%d: %s\n", word_number, current_word);
                }
                has_uppercase = 0; // Reset for next word
            }
        } else {
            state = IN;
            current_word[char_index++] = c;
            if (isupper(c)) {
                has_uppercase = 1;
            }
        }
    }

    // Handle the last word if input doesn't end with whitespace
    if (state == IN) {
        current_word[char_index] = '\0';
        word_number++;
        if (has_uppercase) {
            printf("序号%d: %s\n", word_number, current_word);
        }
    }

    return 0;
}

How this works:

  • We use a state flag to track if we're inside a word or not
  • As we read each character, we build the current word and check if it has any uppercase letters
  • When we hit whitespace (end of a word), we check if the word had uppercase and print its number and text if it does
  • We also handle the edge case where the input ends with a word (no trailing whitespace)
需求2:排查统计大写字母数量及找出包含大写最多单词的代码问题

Let's break down what's wrong with your original code, then fix it:

Key issues in your code:

  1. Broken word detection: Your condition if (c != ' ' || c != '\n' || c != '\t') is always true—no character can be all three whitespace types at once. This means state never switches to OUT, so you never detect word boundaries.
  2. Mixed-up counting logic: You're using uc to track both current word uppercase count and total uppercase, and the line if (uc > word) uc = word; is backwards (you want to update the max value, not overwrite the current count).
  3. No reset for word-specific counts: You never reset uc when a word ends, so it just accumulates all uppercase letters in the text instead of per word.
  4. Unclear variable names: word is used to track max uppercase count, which is confusing (it sounds like it should count words).

Fixed code:

#include <stdio.h>
#include <ctype.h>

#define IN 1  // Inside a word
#define OUT 0 // Outside a word

int main() {
    int c, state = OUT;
    int current_word_uc = 0; // Uppercase count for the current word
    int max_uc = 0;          // Highest uppercase count across all words
    int total_words = 0;     // Optional: track total number of words

    while ((c = getchar()) != EOF) {
        if (isspace(c)) {
            if (state == IN) {
                // End of a word—update max if current word has more uppercase
                if (current_word_uc > max_uc) {
                    max_uc = current_word_uc;
                }
                current_word_uc = 0; // Reset for next word
                state = OUT;
                total_words++;
            }
        } else {
            state = IN;
            if (isupper(c)) {
                current_word_uc++;
            }
        }
    }

    // Handle the final word if input doesn't end with whitespace
    if (state == IN) {
        if (current_word_uc > max_uc) {
            max_uc = current_word_uc;
        }
        total_words++;
    }

    printf("包含大写字母最多的单词有%d个大写字母\n", max_uc);
    return 0;
}

What we changed:

  • Used isspace() to cleanly detect all whitespace characters (more reliable than manual checks)
  • Split counting into current_word_uc (per-word count) and max_uc (running maximum)
  • Reset current_word_uc every time a word ends
  • Added handling for the final word in case input doesn't end with whitespace
  • Renamed variables to make their purpose obvious

内容的提问来源于stack exchange,提问作者Jamal Gains

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:02:39