strcopy, strncopy

I'm trying to create my own versions of a bunch of C string functions.

I have strcmp(), strncmp(), strcat() and strlen() working but I can't get strcopy() or strncopy() (I called them xcopy instead of xcpy for a reason).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* Copies src to the beginning of dest */
char* strcopy(char* dest, const char* src) {
    while (*dest) {
        if (*(dest + 1)) { /* Make sure src does not overlap dest */
            *(dest + 1) = *(dest);
        }

        *dest = *src;
        dest++;
        src++;
    }
    
    return dest;
}

/* Copy the first /n/ chars of src to the first /n/ chars of dest */
char* strncopy(char* dest, const char* src, const int n) {
    int i = 0;
    for (; i < n; i++, dest++, src++) {
        if (*(dest + 1)) { /* Make sure src does not overlap dest */
            *(dest + 1) = *(dest);
        }
        *dest = *src;
    }
    return dest;
}


I've been trying to figure out what I'm doing wrong for the last 25 minutes, but I just can't see where I'm going wrong.

I don't get any errors; but in my main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int main() {
    const char* str1 = "String 1";
    const char* str2 = "String 2";
    char str3[64] = "String 3 ";
    char str4[64] = "String 4";
    
    int a, b, i, j = i = b = a = 0;

    printf("Comparing str1 & str2\n");
    a = mstrcmp(str1, str2);     /* This should = 1 ('2' - '1' == 1) */
    
    printf("Comparing first 7 chars of str1 & str2\n");
    b = mstrncmp(str1, str2, 7); /* This should = 0 */

    printf("Getting lengths of str1, str2\n");
    i = mstrlen(str1);
    j = mstrlen(str2);
    
    printf("Concatenating str3 & str4\n");
    mstrcat(str3, str4);    
    /* Works fine up to here */
    printf("Doing strcopy on str4");
    strncopy(str4, "This is ", 8);

    printf("\nDone.\nResults:"
           "\n\t%d\n\t%d\n\t%d\n\t%d"
           "\n\t%s\n\t%s\n",
           a, b, i, j, str3, str4
          );

    fflush(stdout);
    return 0;
}
Comparing str1 & str2
Comparing first 7 chars of str1 & str2
Getting lengths of str1, str2
Concatenating str3 & str4
Doing strncopy on str4
Done.
Results:
	1
	0
	8
	8
	String 3 String 4
	This is 


something seems to be going wrong.

I get the same result regardless of which function I use...

Could someone explain what I'm doing wrong here? Thanks. I have a feeling that It's because I need to push everything currently in dest to the next space in memory.

Edit: I just realised I wasn't incrementing src in strcopy().
Last edited on
Your overlap check doesn't make much sense. I'd imagine it being a check on addresses rather than their contents.
this doens't make sence:


1
2
3
if (*(dest + 1)) { /* Why are you doing this?? */
            *(dest + 1) = *(dest);
        }


@kbw
:S thanks, I'll try that.

@Hannes
this doens't make sence:

That first line (if statement) is checking that *(dest + 1) != '\0' (EOL). If it does not, then I move the value of *dest to the next space in memory.

Agreeably it isn't a good way of doing it...
Topic archived. No new replies allowed.