String Length in C

Level 4136 keystrokes

Calculates string length manually by iterating until null terminator.

C code

// Declare and initialize a character array (string)
1char str[] = "Hello, World!";
// Initialize length counter to 0
2int len = 0;
// Loop until we find null terminator '\0'
3while (str[len] != '\0') {
// Increment length for each character
4 len++;
5}
// Print the original string using %s format
6printf("String: %s\n", str);
// Print the calculated length (13)
7printf("Length: %d\n", len);

Output

String: Hello, World!
Length: 13
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
  • \× 3
  • =× 3
  • :× 2
  • !× 2
  • '× 2

More C examples at level 4

← C typing practice