Implement a String Copy function in C – which is commonly asked during the coding interview for embeded programming jobs. In C, we use char * to represent the string (char array). And a string is ended with \0 character.
C Implementation of Copy String
The following C function takes a source and copy its content to the pre-allocated target. If the target does not have enough space pre-allocated, a memory access violation will occur.
1 2 3 4 5 6 7 8 | void copyString(char *target, char *source) { while (*source != '\0') { *target = *source; source ++; target ++; } *target = '\0'; } |
void copyString(char *target, char *source) { while (*source != '\0') { *target = *source; source ++; target ++; } *target = '\0'; }
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
205 wordsloading...
Last Post: Math Algorithm to Count the Number of Palindromes Made From Letters
Next Post: Shortcuts and Tips for Mac Beginners (Switching from Windows/Linux)