71. Implement Custom strcpy function

#include <stdio.h>

void custom_strcpy(char *dest, const char *src) {
    int i = 0;
    while (src[i] != '\0') {
        dest[i] = src[i];  // Copy character
        i++;
    }
    dest[i] = '\0';  // Add null terminator
}

int main() {
    char src[101];
    char dest[101];
    fgets(src, sizeof(src), stdin);

    // Remove newline
    int i = 0;
    while (src[i] != '\0') {
        if (src[i] == '\n') {
            src[i] = '\0';
            break;
        }
        i++;
    }

    custom_strcpy(dest, src);
    printf("%s", dest);
    return 0;
}

What is this about?

This simulates what strcpy() does internally — character-by-character copy until it hits the null terminator '\0'.

Why it matters in firmware?

  • string.h may be unavailable in minimal setups
  • Knowing how string copying works helps avoid buffer overflow bugs
  • Used in command parsing, config copying, etc.

Solution Logic

  • Copy each character from src[i] to dest[i]
  • Stop when '\0' is reached
  • Ensure null terminator is added to the destination
     
Loading...

Input

firmware

Expected Output

firmware