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

C语言大小写转换程序遇符号输出乱码问题求助

Fixing Random Garbage Output in C Case-Conversion Program with Symbols

Hey there! Let’s figure out why your case-conversion program is spitting out random gibberish when you input symbols like . or -. I’ve helped folks troubleshoot this exact issue before, so let’s break down the most likely culprits and how to fix them.

Common Causes & Solutions

1. You’re Forgetting the String Terminator (\0)

C strings rely on a null terminator (\0) to tell programs where the string ends. If you don’t add this correctly, your program will keep reading random memory data past your actual string, which shows up as weird symbols.

  • How it happens: Maybe you’re looping through characters but don’t explicitly set output[len] = '\0' after processing all characters. Or your input buffer is too small, causing the terminator to get overwritten.
  • Fix: Always calculate the length of your input string, process each character up to that length, then manually add the null terminator to your output array.

2. You’re Modifying Non-Alphabet Characters

If your code is blindly adding/subtracting 32 (the ASCII difference between upper and lowercase letters) to every character, symbols like . or - will get converted to unprintable or random-looking characters. For example, . has an ASCII value of 46—subtract 32 and you get 14, which is a non-printable control character.

  • How it happens: Your loop doesn’t check if a character is actually an uppercase (A-Z) or lowercase (a-z) letter before modifying it.
  • Fix: Add conditional checks to only convert alphabet characters. Leave all other symbols exactly as they are.

Working Example Code

Here’s a revised version of your program that addresses both issues:

#include <stdio.h>
#include <string.h>

int main() {
    char input[100];
    char output[100];
    int str_length, i;

    printf("Enter your string (can include symbols): ");
    // Use fgets instead of scanf to safely read strings with spaces/symbols
    fgets(input, sizeof(input), stdin);

    // Remove the newline character that fgets appends
    str_length = strlen(input);
    if (str_length > 0 && input[str_length - 1] == '\n') {
        input[str_length - 1] = '\0';
        str_length--;
    }

    // Process each character
    for (i = 0; i < str_length; i++) {
        if (input[i] >= 'A' && input[i] <= 'Z') {
            // Convert uppercase to lowercase
            output[i] = input[i] + 32;
        } else if (input[i] >= 'a' && input[i] <= 'z') {
            // Convert lowercase to uppercase
            output[i] = input[i] - 32;
        } else {
            // Keep symbols/numbers/spaces as-is
            output[i] = input[i];
        }
    }
    // Critical: Add the null terminator to the output string
    output[str_length] = '\0';

    printf("Converted result: %s\n", output);
    return 0;
}

Key Notes About This Code

  • fgets is safer: Unlike scanf("%s"), it reads the entire line (including spaces) and prevents buffer overflow by limiting input to the size of your array.
  • Newline handling: fgets adds a \n at the end of input, so we remove it to avoid extra line breaks in the output.
  • Explicit null terminator: We manually set output[str_length] = '\0' to ensure the string ends correctly.

Give this a try—your symbols should stay intact, and no more random garbage output!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:28:37