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

C语言:优化整数转字符串替换代码及解决连续数字重复转换问题

Optimizing Your C Digit-to-String Converter

Great question! Let's fix both the redundant code and the consecutive duplicate digit issue without using any functions beyond getchar() and putchar().

Step 1: Cut the Redundant Code

Your original code has identical loops for each digit in those if-else blocks—we can eliminate all that repetition by converting the input character directly to an index for your string array. Here's how:

  • Since input digits are characters like '0' to '3', subtract '0' to get an integer between 0-3 (e.g., '1' - '0' = 1).
  • Use that integer to grab the corresponding string from your arr array, then run a single output loop instead of four copies.

Step 2: Handle Consecutive Duplicate Digits

To stop repeating the string for the same consecutive digits, we need to track the last digit we processed. If the current digit is the same as the last one, we skip converting it to a string. We'll also need to reset this tracker when we encounter non-digit characters.

The Optimized Code

#include <stdio.h>

int main() {
    int c;
    // Store our digit strings once, no changes needed here
    const char* arr[4] = {"zero", "one", "two", "three"};
    // Track the last digit we processed; initialize to a value outside 0-3
    int last_digit = -1;

    while ((c = getchar()) != EOF) {
        // Check if we have a valid 0-3 digit
        if (c >= '0' && c <= '3') {
            int current_digit = c - '0';
            // Only process if this digit is different from the last one
            if (current_digit != last_digit) {
                const char* str = arr[current_digit];
                // Single loop to output the string
                for (int i = 0; str[i] != '\0'; i++) {
                    putchar(str[i]);
                }
                // Update the last digit tracker
                last_digit = current_digit;
            }
        } else {
            // Output non-digit characters as-is
            putchar(c);
            // Reset last_digit since we're no longer in a sequence of digits
            last_digit = -1;
        }
    }
    return 0;
}

Key Improvements Explained

  • No More Redundant Loops: We now have one single loop to output the digit string instead of four identical copies. That makes the code shorter and easier to maintain—if you ever need to adjust how strings are printed, you only change one spot.
  • Consecutive Duplicate Fix: The last_digit variable keeps track of the last valid digit we converted. If the next input is the same digit, we skip the conversion. When we hit a non-digit, we reset last_digit so the next valid digit gets processed normally.
  • Const Correctness: We added const to the string pointers since we don't modify those strings—this is a good practice in C to avoid accidental modifications.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:11:31