Search code examples
cfor-loopinsertc-strings

Insert a string to another string in C


I need to insert a character string to another character string.
I'd write my own code and it's work very well until I put SPACE character in insert character string.
I need to find out what is the problem of my code and How I can fix it.

//Insert string in another string
#include <stdio.h>

//Function to count number of characters in a string
int numOfChars(const char string[]);
//Function to insert string to another string
void insertString(char string[], char insert[], int position);

int main()
{
    char str[20]= "145";

    insertString(str, "23", 1);

    printf("%s\n", str);

    return 0;
}

int numOfChars(const char string[])
{
    int counter = 0;

    while(string[counter] != '\0')
        counter++;

    return counter;
}

void insertString(char string[], char insert[], int position)
{
    int i, j;
    int lenght1 = numOfChars(string);
    int lenght2 = numOfChars(insert);

    int finalLenght = lenght1 + lenght2;

    for(i=lenght1, j=0; i<finalLenght; i++, position++, j++)
    {
        string[i] = string[position];
        string[position] = insert[j];
    }

    string[finalLenght] = '\0';

}

Example 1 :
Main string : 145
Insert string : 23
Position : 2
Result : 12345

Example 2 with blank space : Main string : 145
Insert string : 23[SPACE]
Position : 2
Result : 123 54


Solution

  • This loop

    for(i=lenght1, j=0; i<finalLenght; i++, position++, j++)
    {
        string[i] = string[position];
        string[position] = insert[j];
    }
    

    is incorrect because independent on the given position all characters from the source string stating at position position are written after the end of the source string.

    Also the function insertString should be declared like

    char * insertString(char string[], const char insert[], size_t position);
    

    Here is a demonstrative program that shows how the function can be implemented.

    #include <stdio.h>
    
    size_t numOfChars( const char s[] )
    {
        size_t n = 0;
    
        while ( s[n] != '\0' ) ++n;
    
        return n;
    }
    
    char * insertString( char s1[], const char s2[], size_t pos )
    {
        size_t n1 = numOfChars( s1 );
        size_t n2 = numOfChars( s2 );
    
        if ( n1 < pos ) pos = n1;
    
        for ( size_t i = 0; i < n1 - pos; i++ )
        {
            s1[n1 + n2 - i - 1] = s1[n1 - i - 1];
        }
    
        for ( size_t i = 0; i < n2; i++)
        {
            s1[pos+i] = s2[i];
        }
    
        s1[n1 + n2] = '\0';
    
        return s1;
    }
    
    int main(void) 
    {
        enum { N = 20 };
        char s1[N] = "123";
    
        puts( insertString( s1, "AB", 0 ) );
    
        char s2[N] = "123";
    
        puts( insertString( s2, "AB", 1 ) );
    
        char s3[N] = "123";
    
        puts( insertString( s3, "AB", 2 ) );
    
        char s4[N] = "123";
    
        puts( insertString( s4, "AB", 3 ) );
    
        return 0;
    }
    

    The program output is

    AB123
    1AB23
    12AB3
    123AB