codescracker
c

C Program to Find Length of String



« Previous Program Next Program »

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

using strlen() Function

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

// Print Length of String using strlen()
// ----codescracker.com----

#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 under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of sample run:

c program find length of string

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

print string length c

The function named strlen() takes one argument as string and will return the length. Therefore, here 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.

without using strlen() Function

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

// Find Length of String without strlen() Function
// ----codescracker.com----

#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

Supply any string input say codescracker and press ENTER key to see its length as output:

print string length without library function c

As in above program, the string was received from the user using scanf() function. Therefore whenever user will provide any string that contains spaces, then only first word will be received and rest gets skipped. Here is the sample run where user entered any string that contains space say codes cracker:

print string length without library c

As you can see, the word before 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 gets() function in place of scanf() to receive the string (along with spaces) from user as shown in the program given below:

// ----codescracker.com----

#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 user enters the string that contains space say codes cracker. Here is the final snapshot of the second sample run:

c find string length without library

Program Explained

using User-defined Function

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

// Find String Length using Function
// ----codescracker.com----

#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

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker