Created
July 23, 2012 14:53
-
-
Save jrenner/3164052 to your computer and use it in GitHub Desktop.
custom strcar() in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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