Menu
CoddyTech

Using strlen()

Part of the Logic & Flow section of Coddy's C journey. Lesson 15 of 63.

You've learned how to read strings from user input, but now you need a way to determine how long those strings are. C provides the strlen() function from the <string.h> library specifically for this purpose.

The strlen() function counts the number of characters in a string, stopping when it reaches the null terminator \0. Importantly, it does not include the null terminator in its count - it only counts the actual visible characters.

#include <stdio.h>
#include <string.h>

char word[] = "Hello";
int length = strlen(word);
printf("Length: %d\n", length);  // Prints: Length: 5

To use strlen(), you must include the <string.h> header at the top of your program. The function takes a string as its argument and returns an integer representing the number of characters before the null terminator.

This function is particularly useful when you need to know how much space a string occupies, validate input length, or perform operations that depend on string size. It's one of the most commonly used string functions in C programming.

challenge icon

Challenge

Easy

Write a C program that reads multiple words from input and analyzes their lengths using the strlen() function. Your program should:

  1. The lines above your code already read an integer n, the number of words to process
  2. For each of the n words:
    • Declare a character array with 50 elements to store the word
    • Read the word using scanf
    • Use strlen() to calculate the length of the word
    • Print the word and its length in the specified format
  3. After processing all words, have the total number of characters across all words in total_characters
  4. Have the length of the longest word in max_length

The lines above your code already include the <string.h> header that strlen() needs.

The lines below your code print total_characters and max_length. Your output should look like this:

Word: [word] - Length: [length]
Word: [word] - Length: [length]
...
Total characters: [total]
Longest word length: [max_length]

For example, if the input is:

3
hello
programming
C

Your output should be:

Word: hello - Length: 5
Word: programming - Length: 11
Word: C - Length: 1
Total characters: 17
Longest word length: 11

This challenge tests your understanding of using strlen() to measure string lengths, processing multiple strings with loops, and performing calculations based on string length data.

Try it yourself

    // TODO: Write your code here
    // Process each word using a loop
    // For each word:
    // - Declare a character array with 50 elements
    // - Read the word using scanf
    // - Calculate length using strlen()
    // - Print the word and its length
    // - Update total_characters and max_length
    
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow

Practice on your own: Online C compiler