String Copy in C

Level 4172 keystrokes

Implements a string copy function using pointers.

C code

// Copy string from src to dest
1void str_copy(char *dest, const char *src) {
// Loop until null terminator
2 while (*src != '\0') {
// Copy one char, then advance both pointers (postfix ++)
3 *dest++ = *src++;
4 }
// The loop skips the terminator, so add it to dest
5 *dest = '\0';
6}
7
// Destination buffer, large enough for "Hello" plus '\0'
8char buffer[20];
// Copy "Hello" to buffer
9str_copy(buffer, "Hello");
// Prints Hello
10printf("%s\n", buffer);

Output

Hello
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • *× 6
  • ;× 5
  • '× 4
  • "× 4
  • (× 4
  • )× 4
  • +× 4
  • ,× 3
  • \× 3
  • =× 3

More C examples at level 4

← C typing practice