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

C语言词袋程序开发求助:双指针函数字符处理问题

Fixing Accidental Space Removal in Your C Double-Pointer String Cleaning Function

Hey there! Let's work through this double-pointer function issue in your C bag-of-words program. You mentioned you've got symbol removal working correctly, but you're accidentally stripping out a necessary space—leading to output where words are squashed together when they should stay separated.

Common Root Cause

Most double-pointer string cleaning logic fails here when it simply skips non-alphanumeric characters without accounting for the spaces around them. For example, if your original code looks something like this:

void clean_sentence(char *str) {
    char *write = str;
    char *read = str;
    while (*read != '\0') {
        // Keep only alphanumerics and spaces
        if (isalnum((unsigned char)*read) || isspace((unsigned char)*read)) {
            *write++ = *read++;
        } else {
            // Skip symbols, but don't handle spacing
            read++;
        }
    }
    *write = '\0';
}

This code will strip out symbols like &%^ but leave you with over2324the instead of over 2324 the—because it removes the symbols but doesn't preserve the word-separating spaces around them.

Corrected Implementation

Here's a revised version that preserves necessary spacing while removing symbols, using a flag to track whether we've already written a space (to avoid duplicates):

#include <ctype.h>

void clean_sentence(char *str) {
    char *write_ptr = str;
    char *read_ptr = str;
    int just_wrote_space = 0; // Track if last written char was a space

    while (*read_ptr != '\0') {
        if (isalnum((unsigned char)*read_ptr)) {
            // Write alphanumeric chars directly, reset space flag
            *write_ptr++ = *read_ptr++;
            just_wrote_space = 0;
        } else if (isspace((unsigned char)*read_ptr)) {
            // Only write one space if we haven't just written one
            if (!just_wrote_space) {
                *write_ptr++ = ' ';
                just_wrote_space = 1;
            }
            read_ptr++;
        } else {
            // Treat symbols as a space (but only if we haven't just written one)
            if (!just_wrote_space) {
                *write_ptr++ = ' ';
                just_wrote_space = 1;
            }
            read_ptr++;
        }
    }

    // Trim any trailing space we might have added
    if (write_ptr > str && just_wrote_space) {
        write_ptr--;
    }
    *write_ptr = '\0';
}

How This Works

  • Alphanumeric characters: Written directly to the output, and we reset the just_wrote_space flag since we're now back to a non-space character.
  • Spaces: We only write a single space if the last character we wrote wasn't already a space—this prevents multiple consecutive spaces from cluttering the output.
  • Symbols: We replace each sequence of symbols with a single space (if we haven't just written one), which preserves the separation between words that the symbols were sitting between.
  • Trailing space cleanup: We check if the last character we wrote was a space and remove it to avoid an extra space at the end of the sentence.

Test Result

Running this function on your input sentence will produce the desired output:

The sentence after the first call is: "The quick brown fox jumped over 2324 the lazy dogs."

Key Notes

  • Make sure to include <ctype.h> for isalnum() and isspace(). Casting to unsigned char prevents undefined behavior with characters that have negative values (like extended ASCII).
  • This function modifies the string in-place, so ensure you're passing a mutable character array (not a string literal, which is read-only).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:21:07