The CopyString Function Implementation in C – Interview Question for Embeded Programming


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';
}
c-copystring-function-interview-question-for-embeded-programming-1024x1017 The CopyString Function Implementation in C - Interview Question for Embeded Programming c / c++ interview questions string

c-copystring-function-interview-question-for-embeded-programming

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
205 words
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)

The Permanent URL is: The CopyString Function Implementation in C – Interview Question for Embeded Programming

Leave a Reply