Custom strcat function using pointers

Hello, and thank you for taking your time to read my post.
I'm an Icelandic IT student, currently learning C++ programming, amongst other things, and this week's subject is pointers. One of last week's assignments was to program a custom function that works the same as strcat, without using any of C++'s libraries. That was fairly easy, so I came up with the following code:
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
size_t strlen2(const char *str)
{
     size_t counter = 0;

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

     return counter;
}

char* strcat2(const char *destination, const char *source)
{
     unsigned destination_length = strlen2(destination),
              combined_length = destination_length + strlen2(source);
     char *combined_string = new char[combined_length + 1];

     for (unsigned i = 0; i < combined_length; i++)
     {
          if (i < destination_length)
               combined_string[i] = destination[i];
          else
               combined_string[i] = source[i - destination_length];
     }
     combined_string[combined_length] = '\0';

     return combined_string;
}

This week's assignment, however, is to edit the code above so that instead of using the array's index brackets, we're supposed to use pointer techniques, and since I don't have a full understanding of pointers yet, I've come here for help. This is my (failed) attempt:
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
size_t strlen2(const char *str)
{
     size_t counter = 0;

     while (*str++)
          counter++;

     return counter;
}

char* strcat2(const char *destination, const char *source)
{
     unsigned destination_length = strlen2(destination),
              combined_length = destination_length + strlen2(source),
              iterator = 0;
     char *combined_string = new char[combined_length + 1];

     while (*combined_string)
     {
          if (iterator++ < destination_length)
               *combined_string++ = *destination++;
          else
               *combined_string++ = *source++;
     }
     *combined_string = '\0';

     return combined_string;
}

The strlen2 function works as intended, but the strcat2 function does not, but I honestly don't see why. Can you help me?
line 18 your while loop does not make much sense (either it never loops or never stops looping).

Your for loop was correct, just reuse it.

By the way, you should know that MyArray[i] is the same as *(MyArray+i).
Your first function can simply be rewritten as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char* strcat2(const char *destination, const char *source)
{
     unsigned destination_length = strlen2(destination),
     combined_length = destination_length + strlen2(source);
     char *combined_string = new char[combined_length + 1];

     for (unsigned i = 0; i < combined_length; i++)
     {
          if (i < destination_length)
               *(combined_string + i) = *(destination +i);
          else
               *(combined_string + i) = *(source + i - destination_length);
     }
     *(combined_string + combined_length) = '\0';

     return combined_string;
}
You're absolutely right, thanks a lot for your help. :)
Topic archived. No new replies allowed.