Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created November 26, 2009 07:17
Show Gist options
  • Select an option

  • Save shaobin0604/243298 to your computer and use it in GitHub Desktop.

Select an option

Save shaobin0604/243298 to your computer and use it in GitHub Desktop.
/*
* Exercise 5-3. Write a pointer version of the function strcat that we
* showed in Chapter 2:strcat(s, t) copies the string t to the end of s.
*/
#include <stdio.h>
void my_strcat(char *s, char *t)
{
while (*s)
s++;
while (*s++ = *t++)
;
}
int main(void)
{
char testbuff[128];
char *test[] =
{
"",
"1",
"12",
"123",
"1234"
};
size_t numtests = sizeof test / sizeof test[0];
size_t thistest;
size_t inner;
for(thistest = 0; thistest < numtests; thistest++)
{
for(inner = 0; inner < numtests; inner++)
{
strcpy(testbuff, test[thistest]);
my_strcat(testbuff, test[inner]);
printf("[%s] + [%s] = [%s]\n", test[thistest], test[inner], testbuff);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment