FURTHER IMPROVEMENTS by using POINTERS
The previous program still
required the use of variables to keep track of string lengths.
Implementing concatenation by the use of pointers eliminates
this, eg,
#include <stdio.h> void concat( char *, char *, char * ); /* this functions copies the strings a and b to the destination string c */ void concat( char *a, char *b, char *c) { while( *a ) { /* while( *c++ = *a++ ); */ *c = *a; ++a; ++c; } while( *b ) { *c = *b; ++b; ++c; } *c = '\0'; } main() { static char string1[] = "Bye Bye "; static char string2[] = "love."; char string3[20]; concat( string1, string2, string3); printf("%s\n", string3); }
USING strcat IN THE LIBRARY ROUTINE string.h
The following program illustrates using the supplied function
resident in the appropriate library file. strcat()
concatenates one string onto another and returns a pointer to the
concatenated string.
#include <string.h> #include <stdio.h> main() { static char string1[] = "Bye Bye "; static char string2[] = "love."; char *string3; string3 = strcat ( string1, string2 ); printf("%s\n", string3); }