C program to find length of a string

Write a C program to find length of a string using loop. How to find length of a string without using in-built library function strlen() in C programming. Effective way to find length of a string without using strlen() function. How to find length of a string using strlen() string function.

Example

Input

Input string: I love programming. I love Codeforwin.

Output

Length of string: 38

Required knowledge

Basic C programming, If else, Loop, String

Logic to find length of a string

In C every string is terminate with a special character NULL character escaped as \0. To find total length of the input string, iterate through string till the last character and on each iteration increment a counter variable.

Below is the step by step descriptive logic to find length of a string.

  1. Input a string from user. Store it in some variable say text.
  2. Initialize a counter variable to zero, say count = 0. Count variable is used to store total number of characters in the string, which is our effective length of string.
  3. To iterate through input string, run a loop from 0 to the last character of string i.e. NULL character. The loop structure should look like for(i=0; text[i]!=’\0′; i++).
  4. Inside the loop increment the counter variable with 1 i.e. count++.

Program to find length of string using for loop

/**
 * C program to find length of a string using for loop
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text[MAX_SIZE]; /* Declares a string of size 100 */
    int i;
    int count= 0;

    /* Input a string from user */
    printf("Enter any string: ");
    gets(text);

    /* Iterate till the last character of string */
    for(i=0; text[i]!='\0'; i++)
    {
        count++;
    }

    printf("Length of '%s' = %d", text, count);

    return 0;
}

The above method to find length of string is simple enough to understand for a beginner. However, it’s not the best method. We can optimize the above program by eliminating the extra count variable and switching to while instead of for loop.

Program to find length of string using while loop

/**
 * C program to find length of a string using while loop
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text[MAX_SIZE]; /* Declares a string of size 100 */
    int index= 0;

    /* Input string from user */
    printf("Enter any string: ");
    gets(text);

    /* Iterate though last element of the string */
    while(text[index] != '\0')
    {
        index++;
    }

    printf("Length of '%s' = %d", text, index);

    return 0;
}

The above program is somewhat optimized than for based approach. However, you can perform few more optimization to the above code. It won’t improve performance but its more geeky. Following optimizations you can perform with while loop.

  • Since, while loop contains single statement. Hence, you can remove curly braces { }.
    while(text[index] != '\0') index++;
  • You can even transform while to an empty loop.
    while(text[++index] != '\0');

    However, when using this method you must initialize index = -1 instead of 0.

/**
 * C program to find length of a string
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text[MAX_SIZE]; /* Declares a string of size 100 */
    int index = -1;

    /* Input string from user */
    printf("Enter any string: ");
    gets(text);

    /* Iterate though last element of the string */
    while(text[++index] != '\0');

    printf("Length of '%s' = %d", text, index);

    return 0;
}

You can further apply pointer arithmetic to count length of string.

Program to find length of string using pointer

/**
 * C program to find length of a string using pointer
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text[MAX_SIZE]; /* Declares a string of size 100 */
    char * str = text; /* Declare pointer that points to text */
    int count = 0;

    /* Input string from user */
    printf("Enter any string: ");
    gets(text);

    /* Iterate though last element of the string */
    while(*(str++) != '\0') count++;

    printf("Length of '%s' = %d", text, count);

    return 0;
}

In C NULL value is represented with 0. Hence, we can trim the extra NULL checking condition. Let us finally re-write program to check length of a string in more geeky way.

Program to find length of a string

/**
 * C program to find length of a string using pointer
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text[MAX_SIZE]; /* Declares a string of size 100 */
    char * str = text; /* Declare pointer that points to text */
    int count = 0;

    /* Input string from user */
    printf("Enter any string: ");
    gets(text);

    /* Iterate though last element of the string */
    while(*(str++)) count++;

    printf("Length of '%s' = %d", text, count);

    return 0;
}

You can also use predefined library function strlen() to find length of string. strlen() is a string library function defined in string.h header file. It returns length of the string.

Program to find length of string using strlen() string function

/**
 * C program to find length of a string using strlen() function
 */

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum size of string

int main()
{
    char text[MAX_SIZE]; /* Declares a string of size 100 */
    int length;

    printf("Enter any string: ");
    gets(text);

    /* Call strlen() function to count length of string */
    length = strlen(text);

    printf("Length of '%s' = %d", text, length);

    return 0;
}

Output

Enter any string: I love programming. I love Codeforwin.
Length of 'I love programming. I love Codeforwin.' = 38

Happy coding 😉