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

不使用<string.h>库删除字符串中的单词,代码报错变量未声明求助

Fixing "start/end undeclared" Error & Building Word Removal Without <string.h>

Hey Rose, I get that pre-exam stress can make even small code issues feel huge—let's fix this together. First, let's tackle the immediate "start/end undeclared" error, then build the full word-removal function you need without relying on <string.h>.

First: Fix the Undeclared Variable Error

In C, you can't reference variables like start or end unless you declare them first. If you're using an older C standard (pre-C99), you have to declare all variables at the top of the function. For newer standards, you can declare them right before using them, but either way, you can't skip this step. For example, in your word-removal function, add int start, end; at the top of the function (or right before you use them) to eliminate the error.

Step 1: Build Custom String Helper Functions

Since we can't use <string.h>, we need to write our own versions of basic string tools:

// Get length of a string (replaces strlen)
int my_strlen(const char *str) {
    int len = 0;
    while (str[len] != '\0') len++;
    return len;
}

// Compare two strings (returns 0 if equal, replaces strcmp)
int my_strcmp(const char *str1, const char *str2) {
    int i = 0;
    while (str1[i] && str2[i]) {
        if (str1[i] != str2[i]) return str1[i] - str2[i];
        i++;
    }
    return str1[i] - str2[i];
}

Step 2: Core Word Removal Function

This function scans the sentence, finds standalone instances of the target word, and removes them—with all variables properly declared to avoid your original error:

void remove_word(char *sentence, const char *word) {
    int sen_len = my_strlen(sentence);
    int word_len = my_strlen(word);
    int i = 0;

    while (i <= sen_len - word_len) {
        // Check if current position matches the target word
        int is_match = 1;
        for (int j = 0; j < word_len; j++) {
            if (sentence[i + j] != word[j]) {
                is_match = 0;
                break;
            }
        }

        // Ensure we're targeting a standalone word (not part of another word)
        if (is_match) {
            int is_word_start = (i == 0) || (sentence[i - 1] == ' ');
            int is_word_end = (i + word_len == sen_len) || (sentence[i + word_len] == ' ');

            if (is_word_start && is_word_end) {
                // Declare start/end here (move to top of function for pre-C99)
                int start = i;
                int end = i + word_len;

                // Remove trailing space to avoid double spaces after deletion
                if (end < sen_len && sentence[end] == ' ') {
                    end++;
                }

                // Shift characters left to overwrite the word
                int j = start;
                while (end <= sen_len) {
                    sentence[j] = sentence[end];
                    j++;
                    end++;
                }

                // Update sentence length and reset index to re-check the area
                sen_len = my_strlen(sentence);
                i = start;
                continue;
            }
        }
        i++;
    }
}

Step 3: Test the Full Solution

Here's a complete program to verify everything works:

#include <stdio.h>

// Include helper functions and remove_word here

int main() {
    char sentence[200] = "I am preparing for exams, my string skills are not great, not great at all";
    char target_word[] = "great";

    printf("Original sentence: %s\n", sentence);
    remove_word(sentence, target_word);
    printf("After removal: %s\n", sentence);

    return 0;
}

Key Tips to Avoid Future Errors

  • Variable Declaration: Always declare variables before using them. For pre-C99 compilers, move all declarations to the top of the function.
  • Null Terminators: Never forget to let strings end with '\0'—without it, your string will have garbage data.
  • Standalone Word Checks: The is_word_start and is_word_end logic prevents accidental deletion of word fragments (e.g., deleting "cat" from "category").

Hope this helps you nail that string handling practice for your exams!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:42:00