String Concatenation in C

Level 4198 keystrokes

Uses strcat in a loop to concatenate multiple strings into one buffer.

C code

// Start with an empty string so strcat has a '\0' to append to
1char result[50] = "";
// Array of string pointers
2char *words[] = {"Hello", " ", "World", "!"};
// Loop through each word
3for (int i = 0; i < 4; i++) {
// strcat appends at the terminating '\0'; result must have room (50 bytes)
4 strcat(result, words[i]);
5}
// Prints Hello World!
6printf("Result: %s\n", result);
// 5 + 1 + 5 + 1 = 12 characters
7printf("Length: %lu\n", strlen(result));

Output

Result: Hello World!
Length: 12
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.

  • "× 14
  • ;× 7
  • ,× 6
  • (× 5
  • )× 5
  • [× 3
  • ]× 3
  • =× 3
  • :× 2
  • {× 2

More C examples at level 4

← C typing practice