michael
michael

Reputation: 87

issue with string copying

I have assignment where I need to copy a string 'src' to a string 'dst' but I need to do this without "string.h" and the strings aren't ending to '\0' instead they are ending to '?' . So this function "es_cat" needs to return the number of characters of the return string and in this case its 26 and that's correct for me. But the problem is that the string "dst" should be the return string after the function is complete and in the function I even put "c = dst" and then print it and it shows me that that's the correct return string but still from the assignment checker I'll get this error :

test_source.c:129:F:test_es_cat:test_es_cat:0: [07_altstring.c] Copied string is incorrect with inputs "Foobar123neljaa?viisibaz" and "56seitseman?kasiysikymppi". Should have been "Foobar123neljaa56seitseman?", you have "Foobar123neljaa?viisibaz"

int es_cat(char *dst, char *src) {
   char c[100] = {dst[0]};
   int i = 0;
   while (*dst != '?') {
       c[i] = *dst;
       dst++;
       i++;
   }
   while (*src != '?') {
       c[i] = *src;
       src++;
       i++;
   }
   c[i] = '?';
   dst = c;
   printf("\n%s", dst);
   return i;
}
int main(void) {
   char lol[256] = "Foobar123neljaa?viisibaz";
   char asd[256] = "56seitseman?kasiysikymppi";
   int res = es_cat(lol, asd);
   printf("\n%d", res);
}

Upvotes: 0

Views: 631

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409176

Remember that in C arguments are passed by value. That means they are copied and the function can modify its own copy as much as it wants without anything happening to the original. When you do dst = c you modify the local copy of dst not what the lol variable points to inside the main function.

Besides that, what you attempt to do is to make dst point to the local variable c (or rather the first element of c). This is problematic since once the function returns c will go out of scope and cease to exist.

To solve this, I recommend you create the array c in the main function, and pass a pointer to its first character as an argument to the function.

If you're supposed to actually copy the string to dst, then first of all you can't have lol being a pointer to a string literals, because literal strings are read only arrays of characters. As read only you can not modify them, and as arrays they have a fixed size that can not be modified. To solve this you could do e.g.

char lol[256] = "Foobar123neljaa?viisibaz";

That makes lol an array whose contents can be modified. And hopefully big enough to contain the full concatenated string. And instead of just copying pointers (what happens with dst = c) you should write directly to dst (or at least copy to it).


As you seem to need hand-holding and can't seem to think for yourself (harsh but that's what it seems like) here's a functioning program which modifies dst in place:

#include <stdio.h>

int es_cat(char *dst, const char *src)
{
    // Save so we can calculate the length later
    const char *orig_dst = dst;

    // First find the insertion point in the destination string
    while (*dst != '\0' && *dst != '?')
    {
        ++dst;
    }

    // Now dst is either pointing to the terminator, or the '?'
    // Copy from src into dst, until the "end" of src
    while (*src != '\0' && *src != '?')
    {
        *dst++ = *src++;
    }

    // Terminate the destination
    *dst = '\0';

    // Calculate the length of the string in dst, and return it
    return dst - orig_dst;
}

int main()
{
    char lol[256] = "Foobar123neljaa?viisibaz";
    const char *asd = "56seitseman?kasiysikymppi";

    int ret = es_cat(lol, asd);
    printf("result = '%s', length = %d\n", lol, ret);

    return 0;
}

See here for it running.

Upvotes: 1

Paresh Dhandhukiya
Paresh Dhandhukiya

Reputation: 212

you have few problems here.first of all take destination string big enough so that it can copy source string if source is bigger,another thing is you need to do strcpy(or memcpy,strcpy is preferred) in es_cat as directly assigning pointer of local array c won't copy whole string in destination,your code might look something like this :

#define STRING_SIZE  1000
int es_cat(char *dst, char *src) {
   char c[STRING_SIZE*2];
   int i = 0;
   while (*dst != '?') {
       c[i] = *dst;
       dst++;
       i++;
   }
   while (*src != '?') {
       c[i] = *src;
       src++;
       i++;
   }
   c[i] = '?';
   memcpy(dst,c,i);
   dst[i+1] = '\0';//add null
   printf("\n%s", dst);
   return i;
}
int main(void) {
   char dest[STRING_SIZE*2] = "Foobar123neljaa?viisibaz";
   char source[STRING_SIZE] = "56seitseman?kasiysikymppi";
   int res = es_cat(lol, asd);
   printf("\n%d", res);
}

Upvotes: 1

Related Questions