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

K.N.King《C语言编程》第12章项目5指针实现句子单词反转程序的转义字符问题排查

解决C语言单词反转程序中的\xxx转义字符问题

Hey there! Let's break down what's causing those weird \xxx characters and fix your code step by step.

首先,那些\xxx是什么?

Those \xxx sequences are octal escape codes for non-printable ASCII characters. They pop up because your code is accidentally reading memory outside your arr array—this uninitialized memory holds random binary values, and when printf tries to print them as characters, it converts unprintable ones into this octal format since it can't display them normally.

你的代码哪里出问题了?

The core bug is array out-of-bounds access. Let's look at this loop logic:

while(p >= arr) {
    while(*--p != ' ' && p != arr);
    // ... print logic
}

After your first input loop, p points to the position right after your last character (where you set *p = '\0'). When you enter the outer loop, *--p moves p to the last valid character. But when p finally reaches arr (the start of the array), the outer loop condition p >= arr is still true—so you run *--p again, pushing p to memory before your array. Reading this off-limits memory gives you those garbage values that turn into \xxx characters.

Also, that printf("\b%c", mark); is risky: using a backspace to delete a trailing space can cause unexpected behavior if there's no space to delete (like when printing the first word). It's better to avoid trailing spaces entirely instead of trying to fix them after the fact.

修复后的代码

Here's a revised version of your code that fixes these issues:

#include <stdio.h>
#include <ctype.h>
#define N 100
int main() {
    char arr[N] = {0};
    char *p, *q, mark = 0;
    int c;
    p = arr;

    // Read input, leave space for null terminator to prevent overflow
    while((c = getchar()) != '\n' && p < arr + N - 1) {
        if(c == '?' || c == '.' || c == '!') {
            mark = c;
            break;
        } else {
            *p++ = c;
        }
    }
    *p = '\0'; // Ensure string is properly terminated

    // Move p to the end of the string (null terminator)
    p = arr;
    while(*p != '\0') {
        p++;
    }

    int first_word = 1; // Track if we're printing the first word (no leading space)
    while(p > arr) {
        // Skip any trailing spaces at the end of the current segment
        while(p > arr && *--p == ' ');
        q = p;
        // Find the start of the current word
        while(q > arr && *(q - 1) != ' ') {
            q--;
        }
        // Print space before words except the first one
        if(!first_word) {
            printf(" ");
        }
        // Print the word
        while(q < p + 1) {
            printf("%c", *q++);
        }
        first_word = 0;
    }

    // Print the sentence terminator if we found one
    if(mark != 0) {
        printf("%c", mark);
    }
    printf("\n");

    return 0;
}

关键修改点说明

  • Prevent array overflow: The input loop now stops at arr + N -1 to leave room for the null terminator, so we don't write past the array's bounds.
  • Avoid out-of-bounds reading: The outer loop condition is now p > arr, so we never move p before the start of the array.
  • Cleaner space handling: The first_word flag ensures we only print spaces between words, no leading or trailing spaces—no more need for the finicky backspace character.
  • Robust word detection: We skip trailing spaces first, then clearly find the start and end of each word to avoid missing any characters or picking up garbage.

Test this with your input My name is jiyong! and you'll get the expected output jiyong is name My! with no weird escape codes.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 06:52:46