C

How To Find Length Of String In C

In this tutorial, we are going to see how to find length of a string in C using strlen() function. The strlen() function takes a single argument, which is the variable whose length is to be found, and returns the length of the string passed.

The strlen() function is defined in the <string.h> header file.


‘\0’ is not counted

 
 

How To Find the Length Of a String In C
#include <stdio.h>
#include <string.h>

int main()
{
    char str1[20]="StackHowTo";
    char str2[20]={'S','t','a','c','k','H','o','w','T','o','\0'};
    char str3[20];

    printf("Length of str1 = %d \n",strlen(str1));
    printf("Length of str2 = %d \n",strlen(str2));
    printf("Enter a string: ");
    gets(str3);
    printf("Length of str2 = %d \n",strlen(str3));
    return 0;
}

Output:

Length of str1 = 10 
Length of str2 = 10 
Enter a string: Hello
Length of str2 = 5

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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