C Program to Find the Length of a String

Introduction

Finding the length of a string involves counting the number of characters in the string, excluding the null terminator (\0). This guide will show you how to write a C program to find the length of a string provided by the user.

Problem Statement

Create a C program that:

  • Takes a string as input from the user.
  • Calculates the length of the string.
  • Displays the length of the string.

Example:

  • Input: String = "Hello"
  • Output: Length = 5

Solution Steps

  1. Include the Standard Input-Output and String Libraries: Use #include <stdio.h> for standard input-output functions and #include <string.h> for string functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare a variable to store the string and a variable to store the length of the string.
  4. Input the String: Use gets or scanf to take input from the user for the string.
  5. Find the Length of the String: Use the strlen function from the string.h library to calculate the length of the string.
  6. Display the Length of the String: Use printf to display the length of the string.

C Program Using strlen Function

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

int main() {
    // Step 1: Declare a variable to hold the string and another for the length
    char str[100];
    int length;

    // Step 2: Prompt the user to enter a string
    printf("Enter a string: ");
    gets(str);  // Using gets to read the string including spaces

    // Step 3: Find the length of the string using strlen
    length = strlen(str);

    // Step 4: Display the length of the string
    printf("Length of the string \"%s\" is: %d\n", str, length);

    return 0;  // Step 5: Return 0 to indicate successful execution
}

C Program Without Using strlen Function

Alternatively, you can find the length of the string manually by iterating through the string until you reach the null terminator (\0).

#include <stdio.h>

int main() {
    // Step 1: Declare a variable to hold the string and another for the length
    char str[100];
    int length = 0;

    // Step 2: Prompt the user to enter a string
    printf("Enter a string: ");
    gets(str);  // Using gets to read the string including spaces

    // Step 3: Find the length of the string manually
    while (str[length] != '\0') {
        length++;
    }

    // Step 4: Display the length of the string
    printf("Length of the string \"%s\" is: %d\n", str, length);

    return 0;  // Step 5: Return 0 to indicate successful execution
}

Explanation

Using strlen Function

  • The strlen function from the string.h library calculates the length of the string by counting the number of characters before the null terminator (\0).

Without Using strlen Function

  • The program manually calculates the length of the string by iterating through each character of the string until it encounters the null terminator (\0). The variable length is incremented during each iteration to keep track of the number of characters.

Steps Explanation:

  1. Declare Variables: The str array is declared to store the input string, and length is used to store the length of the string.
  2. Input the String: The gets function reads the input string, including spaces, and stores it in str.
  3. Calculate the Length: Depending on the approach, either the strlen function or a manual loop calculates the length of the string.
  4. Display the Length: The program displays the calculated length of the string using printf.

Output Example

Example:

Enter a string: Hello
Length of the string "Hello" is: 5

Another Example:

Enter a string: C Programming
Length of the string "C Programming" is: 13

Conclusion

This C program demonstrates two methods to find the length of a string: using the strlen function and manually iterating through the string. It covers basic concepts such as string manipulation, loops, and functions, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top