String Copy in C

Level 3112 keystrokes

Uses strcpy to copy a string from source to destination array.

C code

// Source string: 5 letters plus '\0'
1char src[] = "Hello";
// The destination must be large enough; strcpy does not check the size
2char dest[20];
// Copy src into dest, including the '\0' (destination comes first)
3strcpy(dest, src);
// The source is unchanged
4printf("Source: %s\n", src);
// dest now holds its own copy: Hello
5printf("Dest: %s\n", dest);

Output

Source: Hello
Dest: 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
  • ,× 3
  • (× 3
  • )× 3
  • :× 2
  • [× 2
  • ]× 2
  • \× 2
  • %× 2

More C examples at level 3

← C typing practice