C Program to Find the Length of a String Without Using Library Function

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 without using any library functions like strlen.

Problem Statement

Create a C program that:

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

Example:

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

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output 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 count the length of the string.
  4. Input the String: Use gets or scanf to take input from the user for the string.
  5. Calculate the Length of the String Manually: Use a loop to iterate through the string until the null terminator is encountered, counting each character.
  6. Display the Length of the String: Use printf to display the length of the string.

C Program to Find Length of String Without Using Library Function

#include <stdio.h>

int main() {
    // Step 1: Declare a variable to hold the input string and a variable 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

Step 1: Declare Variables

  • The variable str is declared to store the input string. The variable length is used to count the number of characters in the string.

Step 2: Input the String

  • The program prompts the user to enter a string using printf. The gets function is used to read the string, allowing it to include spaces.

Step 3: Calculate the Length of the String

  • The program uses a while loop to iterate through each character of the string until it encounters the null terminator (\0). The length variable is incremented during each iteration to count the number of characters.

Step 4: Display the Length of the String

  • After calculating the length, the program displays the length of the string using the printf function.

Step 5: Return 0

  • The return 0; statement indicates that the program executed successfully.

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: 14

Conclusion

This C program demonstrates how to find the length of a string manually by iterating through each character until the null terminator is reached. It covers basic concepts such as string manipulation, loops, and character counting, 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