Skip to content

Instantly share code, notes, and snippets.

@jrenner
Created July 23, 2012 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrenner/3164052 to your computer and use it in GitHub Desktop.
Save jrenner/3164052 to your computer and use it in GitHub Desktop.
custom strcar() in C
#include <stdio.h>
#include <string.h>
#define STRINGSIZE 80
int cat(char *a, const char* b, const size_t max_size)
{
//puts("FUNCTION BEGIN");
size_t size = strlen(a);
a += size;
while (*b)
{
if (size >= max_size)
{
*a = '\0';
printf("ERROR: function cat() has run past max_size: size = %lu\n", size);
return -1; // I feel like I should undo the changes
// since I'm returning an error.
}
*a++ = *b++;
size++;
}
*a = '\0';
//puts("FUNCTION FINISHED");
return 0;
}
int main()
{
char strA[STRINGSIZE] = "I am a small cat ";
char strB[STRINGSIZE] = "with whiskers who likes milk.";
printf("A: '%s'\nB: '%s'\n", strA, strB);
puts("-= Let's call cat() =-");
int result = cat(strA, strB, STRINGSIZE);
if (result == -1)
printf("main() - ERROR in function cat(): -1\n");
printf("A: '%s'\nB: '%s'\n", strA, strB);
printf("cat() returned: '%d'\n", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment