loc(20*sizeof(int))创建一个内存块以容纳20个整数,该块的起始地址存储在数组中。使用array后,必须释放(array)将内存返回到系统。char newString[oldStringLen+2]={'\0'}无效语法。可变大小的对象可

loc(20*sizeof(int))创建一个内存块以容纳20个整数,该块的起始地址存储在数组中。使用array后,必须释放(array)将内存返回到系统。char newString[oldStringLen+2]={'\0'}无效语法。可变大小的对象可,c,string,replace,append,C,String,Replace,Append,loc(20*sizeof(int))创建一个内存块以容纳20个整数,该块的起始地址存储在数组中。使用array后,必须释放(array)将内存返回到系统。char newString[oldStringLen+2]={'\0'}无效语法。可变大小的对象可能未初始化 int main(int argc, const char * argv[]) { char *s = "This is a test"; char *newstring = malloc(strlen(s));


loc(20*sizeof(int))创建一个内存块以容纳20个整数,该块的起始地址存储在
数组中。使用
array
后,必须
释放(array)
将内存返回到系统。
char newString[oldStringLen+2]={'\0'}无效语法。可变大小的对象可能未初始化
int main(int argc, const char * argv[]) {

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


    for (int i = 0 ; s[i] != '\0' ; 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;
}
char *s = "This is a test";
int len = 0;

while(s[len++]);//strlen(s) + 1
for (int i = 0 ; s[i] != '\0' ; 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] != '\0' ; 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] = '\0';
puts(newstring);
free(newstring);
int num = 0;
int buf[256];

buf[num++] = 123;
buf[num++] = 456;
buf[num++] = 789;
buf[num++] = 123;
buf[num] = 123;
++num;
#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] != '\0'; ++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] = '\0';

    // 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
// get length of original string
int oldStringLen = myStrLen( s );

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

// 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] );