Write the function my_strncpy(). The function has three parameters: a char * dest, a const char * src, and an int max, which represents the maximum size of the destination buffer. Copy the characters in src to dest, but don't copy more than max-1 characters. Make sure you correctly terminate the copied string. The function returns a pointer to the first character in dest.

Is the for loop I used to empty the string correct?
And will the while loop copy the chars in src to dest?
I ran the code with some tests string, but it didn't gave me what I wanted.

char * my_strncpy(char * dest, const char * src, int max)
{
    int b = 0;
    for(int i = 0; i < max; i++)
    {
        dest[i] = '\0';
    }
    while(b < (max-2))
    {
        *dest = *src;
        src++;
        dest++;
        b++;
    }
    return dest;
}
int main()
{
    char cstr[50] = "Abadabadoo!";
    char buf[10];
    char * cat = "cat";
    cout << "\nmy_strncpy(buf, cstr, 10) expects \"Abadabado\"" << endl;
    cout << "  -- \"" << my_strncpy(buf, cstr, sizeof(buf)) << "\"" << endl;
    cout << "my_strncpy(buf, cat, 10) expects \"cat\"" << endl;
    cout << "  -- \"" << my_strncpy(buf, cat, sizeof(buf)) <<  "\"" << endl;
}

Recommended Answers

All 4 Replies

delete the loop on lines 4-7 because the dest buffer may contain some text that the program needs to keep. That loop destroys all that text.

The first thing the function needs to do is find the end of the text, if any, that is in the dest buffer.

while( *dest != '\0' )
{
   ++dest;
   --max;  
}

After that, it can copy the source to dest. The loop starting on line 8 needs more work too. Its too complicated. You should take no more than three lines of code for that loop. Hint: variable b is not needed.

I made the for loop because the problem tells me to terminate the copied string. Therefore I was thinking about empty it. o_0
and I am really don't know how to properly declared that loop =.=
Please help me with some explanation, i really need to understand pointer for a test =.=

delete the loop on lines 4-7 because the dest buffer may contain some text that the program needs to keep. That loop destroys all that text.

The first thing the function needs to do is find the end of the text, if any, that is in the dest buffer.

while( *dest != '\0' )
{
   ++dest;
   --max;  
}

After that, it can copy the source to dest. The loop starting on line 8 needs more work too. Its too complicated. You should take no more than three lines of code for that loop. Hint: variable b is not needed.

I was able to make it work when using array notation but I want to see how it would be done if using only pointer.

or(int i = 0; i < max; i++)
    {
        dest[i] = '\0';
    }
    for(int  i = 0; i < max-1; i++)
    {
        dest[i] = src[i];
    }

For strncpy() all you have to do is this: You don't have to clear the dest buffer because its just a waste of time and CPU cycles. Also you have to check on the end of src string, which may or may not be longer than max.

int i;
for(i = 0; i < max-1 && src[i] != '\0'; i++)
{
   dest[i] = src[i];
}
dest[i] = '\0'; // NULL terminate the dest buffer
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.