Replacing substrings in C without using string library functions

While this code is disgusting and I clearly solved the problem incorrectly, the code works (for now). I want to communicate with him, but I really don't know what I am doing wrong here. The goal is not to use any built-in functions , take the line "This is a test" and replace "te" with "gho" to spell out "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] != '\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;
}

      

My problem is that I don't know how to add characters to a new string in C so that I can maintain the integrity of the original string by simply replacing individual characters.

+3


source to share


3 answers


We need to recognize the pattern in what you want to do. It’s helpful to remember that you can fill a new array like this:

int num = 0;
int buf[256];

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

      

This is a reduction. Line:

buf[num++] = 123;

      



Provides equivalent behavior for:

buf[num] = 123;
++num;

      

So what do we want to do here? We want to create a new string that is the same as the original, except that it replaces "te" with "gho". So how about this?

#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 make one that 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);
}

      

+1


source


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

      



+2


source


int myStrLen( char * );


int myStrlen( char *testStr )
{
    int rtnCount;

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

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

      

other string functions can be implemented in a similar way.

then the "home grow" line functions can be used like this:

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

      

do i need code to continue searching the original string for other possible replacements?

of course, how to display the result to the user might be "interesting" since C has no way of doing any I / O without using something like the stdio.h library

0


source







All Articles