C program to find the length of a string

In this article, you will learn and get code about how to find and print the length of any given string given by the user (at run-time) using the following approaches:

Print the length of a string using the strlen() function

To find the length of any given string in C programming, you have to ask the user to enter the string and then find its length using the strlen() function of the string.h library, as shown in the program given below:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[20];
    int len;
    printf("Enter the string: ");
    gets(str);
    len = strlen(str);
    printf("\nLength of the string = %d", len);
    getch();
    return 0;
}

As the above program was written in the Code::Blocks IDE, here is the first snapshot of the sample run:

c program find length of string

Now supply any string, say "codescracker," and press the ENTER key to see the length of the given string as shown here in the second snapshot:

print string length c

The function named strlen() takes one argument as a string and returns the length. Therefore, the length of the string, say str, gets returned by the function strlen() and initialized to the variable len. Print the value of len as output.

Find the length of a string without using the strlen() function

This program does not use the strlen() function to find the length; rather, the length gets calculated manually here:

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch[20];
    int count=0, i;
    printf("Enter any string: ");
    scanf("%s", ch);
    for(i=0; ch[i]!='\0'; i++)
        count++;
    printf("\nLength of the String = %d", count);
    getch();
    return 0;
}

Here is its sample run:

c program print string length without library

Enter any string as input, for example, codescracker, and press the ENTER key to see its length as output:

print string length without library function c

As in the above program, the string was received from the user using the scanf() function. As a result, whenever the user enters a string that contains spaces, only the first word is received and the rest is skipped. Here is a sample run in which the user entered any string containing space, such as "codes cracker,"

print string length without library c

As you can see, the word before the space is "codes," therefore the program will only calculate the length of "codes" that will be 5. To solve this problem, we have to use the gets() function in place of scanf() to receive the string (along with spaces) from the user, as shown in the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    char str[100];
    int count=0, i;
    printf("Enter any string: ");
    gets(str);
    for(i=0; str[i]!='\0'; i++)
        count++;
    printf("\nLength of the String = %d", count);
    getch();
    return 0;
}

Here is the final snapshot of the first sample run:

print string length without library c

Now let's take another sample run where the user enters a string that contains spaces, say "codes cracker." Here is the final snapshot of the second sample run:

c find string length without library

Program Explained

Find string length using a user-defined function

This program is made using user-defined functions to do the same job as the previous one:

#include<stdio.h>
#include<conio.h>
int findStringLen(char str[50]);
int main()
{
    char str[50];
    int len;
    printf("Enter any string: ");
    gets(str);
    len = findStringLen(str);
    printf("\nLength of the String = %d", len);
    getch();
    return 0;
}
int findStringLen(char s[50])
{
    int i, count=0;
    for(i=0; s[i]!='\0'; i++)
        count++;
    return count;
}

Here is the final snapshot of the sample run:

print string length using function c

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!