C bootcamp · Lab 36

String length

easyStrings8 minLesson: C Strings

Read the question, write C on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read one word and print how many characters it has.

Walk the char array until you hit the null byte. Do not call strlen — count it yourself so you see how C strings work.

Input. One word.

Output. One integer: the length.

Examples

Example 1
Input
hello
Output
5
Example 2
Input
C
Output
1
Hint
  1. int n = 0; while (word[n] != '\0') n++;
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <stdio.h>

int main(void) {
  char word[64];
  if (scanf("%63s", word) != 1) return 1;
  int n = 0;
  while (word[n] != '\0') n++;
  printf("%d\n", n);
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.