为什么我的删除功能中存在错误

why there is an error in my removestring function

本文关键字:存在 错误 功能 我的 删除 为什么      更新时间:2023-10-16

我正在制作一个称为 removeString()的函数。目的是从文本中删除字符串

示例:如果文本是"错误的儿子",我想删除"错误"为"儿子",我使用此功能。

原型是:

void removeString(char source[], int start, int numofchrem);

和这样的程序就是这样:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void removeString(char source[], int start, int numofchrem)
{
    int i, j, k;
    char n[20];
    j = start + numofchrem - 1;
    for (i = 0; i < numofchrem; i++)
    {
        if (source[start] == '')
            break;
        source[j] = 'b';
        j--;
    }
    printf("%s", source);
}
int main()
{
    char text[] = "the wrong son";
    void removeString(char source[], int start, int numofchrem);
    removeString(text, 4, 6);
}

当我制作程序时,我首先调试源中的字符,就像这个

"the bbbbbbson"

当我使用以下方式打印文本时:

printf("%s",source); 

该程序仅显示"儿子"而不是"儿子"。因此,如果有人能帮助我,我会非常感激。

您需要将"son"(和''(移回,以替换的内容

void removeString(char source[], int start, int numofchrem)
{
    char *d = source + start; // d points to "wrong..."
    char *s = source + start + numofchrem; // s points to "son"
    while (*s != '') {
        *d = *s; // replace 'w' with 's', 'r' with 'o', ...
        d++; // point to next: "rong...", "ong...", "ng..."
        s++; // point to next: "on", "n"
    }
    *d = ''; // terminate string
}