Last active
December 15, 2015 19:19
-
-
Save khajavi/5310159 to your computer and use it in GitHub Desktop.
c-string append (concat)
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> | |
#include <stdlib.h> | |
const char* append( const char* str1, const char* str2 ) { | |
const size_t str1_length = strlen( str1 ); | |
const size_t str2_length = strlen( str2 ); | |
const size_t total_length = str1_length + str2_length; | |
char *const str_buff =(char*) malloc( total_length + 1 ); | |
if ( str_buff == NULL ) { | |
fprintf( stderr, "malloc failed\n" ); | |
exit( EXIT_FAILURE ); | |
} | |
strcpy( str_buff, str1 ); | |
strcpy( str_buff + str1_length, str2 ); | |
return str_buff; | |
} | |
int main( void ) { | |
puts( append( "Milad", " Khajavi" ) ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment