在不使用字符串库函数的情况下替换 C 中的子字符串

Replacing substrings in C without using string library functions

虽然这段代码很可怕,而且我已经清楚地错误地解决了这个问题,但这段代码(暂时)有效。我想弄乱它,但我真的不知道我在这里做错了什么。目标是 不使用任何内置函数 ,获取字符串 "This is a test" 并将 "te" 替换为 "gho" 以拼写 "This is a ghost".

int main(int argc, const char * argv[]) {

    char *s = "This is a test";
    char *newstring = malloc(strlen(s));


    for (int i = 0 ; s[i] != '[=10=]' ; i++){
        if (s[i] == 't' && s[i+1] == 'e') {
            newstring[i] = 'g';}
        else if (s[i] == 'e' && s[i+1] == 's') {
            newstring[i] = 'h';}
        else if (s[i] == 's' && s[i+1] == 't') {
            newstring[i] = 'o';
        }
        else if (s[i] == 't') {
            newstring[i] = 's';
        }
        else {
            newstring[i] = s[i];
        }
        }
    printf("%st",newstring);


    return 0;
}

我的问题是我不知道如何在 C 中将字符追加到新字符串,以便在仅替换单个字符的同时保持原始字符串的完整性。

char *s = "This is a test";
int len = 0;

while(s[len++]);//strlen(s) + 1
for (int i = 0 ; s[i] != '[=10=]' ; i++){
    if (s[i] == 't' && s[i+1] == 'e') {//count "te"
        ++len;
        ++i;
    }
}

char *newstring = malloc(len);
int j = 0;
for (int i = 0 ; s[i] != '[=10=]' ; i++){
    if (s[i] == 't' && s[i+1] == 'e') {
        newstring[j++] = 'g';
        newstring[j++] = 'h';
        newstring[j++] = 'o';
        ++i;
    } else {
        newstring[j++] = s[i];
    }
}
newstring[j] = '[=10=]';
puts(newstring);
free(newstring);

我们应该认识到你想做的事情的模式。首先,记住您可以像这样填写一个新数组是一个有用的技巧:

int num = 0;
int buf[256];

buf[num++] = 123;
buf[num++] = 456;
buf[num++] = 789;

这是一个shorthand。该行:

buf[num++] = 123;

提供与以下内容等效的行为:

buf[num] = 123;
++num;

那么我们想在这里做什么?我们想创建一个与原始字符串相同的新字符串,只是将 "te" 替换为 "gho"。那么这个怎么样?

#include <stdio.h>

int main(int argc, const char * argv[]) 
{
    const char *s = "This is a test";
    int i = 0;
    int new_len = 0;

    // We don't know what the final size of the string will be, so
    // let's make one that's generally large enough. Can do more
    // elaborate things here if you want a really robust solution.
    char new_string[256] = {0};

    // For each character in the string:
    for (i=0; s[i] != '[=13=]'; ++i)
    {
        if (s[i] == 't' && s[i+1] == 'e')
        {
            // If we find "te", add "gho".
            new_string[new_len++] = 'g';
            new_string[new_len++] = 'h';
            new_string[new_len++] = 'o';

            // ... and skip one character to ignore both the 't' and the 'e'.
            ++i;
        }
        else
        {
            // Otherwise just add the same character.
            new_string[new_len++] = s[i];
        }
    }

    // Add the null terminator at the end of our new string.
    new_string[new_len] = '[=13=]';

    // Output the new string.
    printf("%s\n", new_string);
}
int myStrLen( char * );


int myStrlen( char *testStr )
{
    int rtnCount;

    for( rtnCount = 0; testStr[rtnCount]; rtnCount++ ) {;}

    return( rtnCount );
} // end function: myStrlen

其他字符串函数可以类似的方式实现。

然后 'home grown' 字符串函数可以像这样使用:

// get length of original string
int oldStringLen = myStrLen( s );

// provide room on the stack for the new string
char newString[oldStringLen+2] = {'[=11=]'};

// search for occurrence of string to be replaced
char * targetString = myStrStr( s, "test" );
if( NULL == targetString )
{ // then target string not found
    // handle error??
    return( -1 );
}

// implied else, target string found in 's'

// copy first part of original string
myStrNCpy( newString, s, (targetString - s) +1 );

// append the replacement string
myStrCat( newString, "ghost" );

// append remainder of original string
myStrCat( newString, &targetString[4] );

代码是否需要继续在原始字符串中查找 其他可能的替代品?

当然,如何将结果显示给用户 可能是 'interesting' 因为 C 没有能力执行任何 I/O 不使用 stdio.h 库

之类的东西