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

C语言交换法排序大小写字符串时出现字符串截断问题求助

Hey there! Let's dig into why your swap-based sort is truncating your string to just "AAC" instead of giving you the expected "AACCbb". This is almost always related to accidentally messing with the string's null terminator ('\0') or using incorrect loop boundaries—let's break it down.

First, Let's Diagnose the Likely Issue

The most common culprit here is that your sorting loop is accidentally accessing the string's null terminator ('\0') and swapping it into the middle of your character array. Since printf stops printing as soon as it hits '\0', any characters after that get cut off completely.

For example, if your loop runs one iteration too far (like using j <= len instead of j < len), you'll end up grabbing the '\0' at the end of the string and swapping it into position 3 of your "AAbbCC" input. That turns your array into A A C \0 b b, so printf only outputs the first three characters.

Here's the Fixed Swap-Sort Implementation

Let's write a working version using bubble sort (a common swap-based method) that avoids this problem, then walk through the key fixes:

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

void sortCaseAwareString(char str[]) {
    if (str == NULL) return; // Guard against null pointers
    
    int strLength = strlen(str);
    // Outer loop: iterate through each position to place the correct character
    for (int i = 0; i < strLength - 1; i++) {
        // Inner loop: find the smallest remaining character to swap into position i
        for (int j = i + 1; j < strLength; j++) {
            // ASCII values make this easy: uppercase (65-90) < lowercase (97-122)
            // Direct comparison will naturally group uppercase first, then lowercase
            if (str[j] < str[i]) {
                // Standard swap operation
                char temp = str[i];
                str[i] = str[j];
                str[j] = temp;
            }
        }
    }
}

int main() {
    char inputStr[] = "AAbbCC";
    printf("Original string: %s\n", inputStr);
    
    sortCaseAwareString(inputStr);
    printf("Sorted string: %s\n", inputStr);
    
    return 0;
}

Key Fixes & Notes to Avoid Future Issues

  • Use strlen() for string length: Never use sizeof() on a string passed as a function parameter—sizeof() will return the size of the pointer, not the actual array length. strlen() correctly counts only the valid characters (excluding '\0').
  • Stick to valid character indices: The loop boundaries i < strLength - 1 and j < strLength ensure we never touch the null terminator at str[strLength]. This prevents accidental truncation.
  • Add null pointer guard: Checking if (str == NULL) makes your function more robust, avoiding crashes if someone passes an invalid pointer.

Testing the Code

When you run this, you'll get exactly the output you want:

Original string: AAbbCC
Sorted string: AACCbb

That should fix your truncation issue and give you the correct sorted string!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:26:02