Deepak
Deepak

Reputation: 3

I am getting problem with concatenation of strings

In the below for loop when i updating s1 using s2, string length of s1 is not updated.

#include<stdio.h>
#include<string.h>
int main()
{
        char s1[20]="this is",s2[10]="book";
        printf("%ld\n",strlen(s1));
        for(int i=0;i<strlen(s2);i++)
        {
                printf("%ld\n",strlen(s1));
                s1[strlen(s1)+i]=s2[i];
                printf("%ld\n",strlen(s1));
        }
        printf("%s\n",s1);
}

Output:

7 7 8 8 8 8 8 8 8 this isb

string length of s1 is updated only one time and after that it is not updated because of that concatenation getting error.

How can we ensure this?

Upvotes: 0

Views: 127

Answers (6)

0___________
0___________

Reputation: 67546

here you have a very simple one. But fast and efficient :)

char *concat(char *s1, const char *s2)
{
    char *saved = s1;           // save it for the return
    if(s1 && s2)                // check if both are not NULL
    {
        while(*s1) s1++;        // go to the end of string s1
        while((*s1++ = *s2++)); // append second string s2
        //*s1 = 0;                // 0 terminate the concatenated string
    }
    return saved;
}

If you do not understand something - ask.

You can experiment yourself here https://onlinegdb.com/B1mE7GaJU

Upvotes: 2

Ahmed Yasen
Ahmed Yasen

Reputation: 145

The trick is in this line s1[strlen(s1)+i]=s2[i];

Lets track the loop to catch the bug

  • When i = 0 and s1[] = "This is"
    so s[strlen(s1)+i] = s2[i] or s[7] = s[0] the result is s[] = "This isb
  • when i = 1 and s[strlen(s1)+i] = s[i] or s[8 + 1] = s[1] the result is s[] = "This isb\0o" (note the character 'o' is after the null character)

The trick is here you are summing the string s1 after the size is changed and now a wrong position to the new character

Solution: simply rewrite this line s1[strlen(s1)+i]=s2[i]; to be s1[strlen(s1)]=s2[i];

Upvotes: 0

KamilCuk
KamilCuk

Reputation: 141145

What you want to do is:

#include <stdio.h>
#include <string.h>
#include <assert.h>
int main()
{
        char s1[20]="this is",s2[10]="book";
        printf("%ld\n",strlen(s1));

        // some temporary variable
        const size_t t = strlen(s1);

        for(int i = 0; i < strlen(s2); i++)
        {
                printf("%ld\n",strlen(s1));

                // some robust protection against buffer overflow
                assert(t + i + 1 < sizeof(s1));
                // add the character 
                s1[t + i] = s2[i];
                // adding the null terminator is not needed
                // when there are fewer characters in string literal then array size
                // the remainder is initialized to 0 implicitly
                // s1[t + i + 1] = '\0';

                printf("%ld\n",strlen(s1));
        }
        printf("%s\n",s1);
}

But really, just:

strcat(s1, s2);

Upvotes: 0

H S
H S

Reputation: 1221

This is because you're calculating strlen(s1) again and again. When :

  1. i = 0 strlen will give 7, thus s1[7 + 0] = 'b'
  2. i = 1 strlen will give 8, thus s1[ 8 +1 ] = 'o'

As you may see, you have updated s1[9] directly and skipped s1[8], leading to aforementioned error.

    char s1[20]="this is",s2[10]="book";
    printf("%ld\n",strlen(s1));
    size_t offset = strlen(s1);
    for(int i=0;i<strlen(s2);i++)
    {
            printf("%ld\n",strlen(s1));
            s1[ offset + i ]=s2[i]; //try this
            printf("%ld\n",strlen(s1));
            s1[ offset + i + 1] = '\0';

    }
    printf("%s\n",s1);

Upvotes: 0

Nikhil Vas
Nikhil Vas

Reputation: 1

You can use try this,

#include<stdio.h>
#include<string.h>
int main()
{
        char s1[20]="this is",s2[10]="book";
        printf("%ld\n",strlen(s1));
        for(int i=0;i<strlen(s2);i++)
        {
                printf("%ld\n",strlen(s1));
                s1[strlen(s1)]=s2[i];
                printf("%ld\n",strlen(s1));
        }
        printf("%s\n",s1);
}

Since the length is updated each time youre in loop, you can get rid of i.

Upvotes: 0

serg06
serg06

Reputation: 2695

Save strlen(s1) in a variable before starting the loop, then use that variable in s1[var+i]=s2[i];.

Also don't forget to null-terminate your new string. An easy way to do this would be to include s1[var+i+1]='\0'; inside the loop.

Upvotes: 1

Related Questions