strlen in c

If you want to know about strlen in c then read this blog post completely.

What is strlen in c?

Strlen in c is used to calculate the length of a string.

The computing of string length does not include terminate character.

It counts the number of characters in a given string and returns the integer value.

This function is declared in string.h header file.

Prototype

int  strlen(const char*strng);

Parameters for strlen in c

Here it calculates the length of a given string.

Return value of strlen in c

This function returns the length of a given string.

Example program in c

Example 1

//strlen example program 1
#include<stdio.h>
#include<string.h>
int main()
{
  char word[]={'H','E','L','L','O','\0'};
  printf("\n\n\tThe length of a string is %d \n\n",strlen(word));
  return 0;
}
strlen example in c 1
strlen example in c 1

Code explanation

Here we include string.h header inorder to execute strlen() function.

In the program, we initialized a string ab with the value “HELLO”. With the help of a strlen() function, we calculate the total length of a string ab and print it to a console.

Example 2

//strlen example program 2
#include<stdio.h>
#include<string.h>
int main()
{
  char word[100];
  printf("\n\n\tEnter a word.\n\t");
  scanf("%s",word);
  printf("\n\tThe length of word is %d \n\n",strlen(word));
  return 0;
}
strlen example in c 2
strlen example in c 2

Code explanation

Here we are taking input word from user. And we are calling strlen with word as parameter.

While compiling the code you may encounter following errors. Just ignore it.

example 2.c: In function ‘main’:
example 2.c:8:38: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ {aka ‘long unsigned int’} [-Wformat=]
    8 |   printf("\n\tThe length of word is %d \n\n",strlen(word));
      |                                     ~^       ~~~~~~~~~~~~
      |                                      |       |
      |                                      int     size_t {aka long unsigned int}
      |  

Difference between strlen and strcat in c

strlen in c is used to calculate the length of a string. strcat is used to append source string to destination string.

strlen returns total length of a string. strcat returns a pointer to the final string.

strlen terminates when it encounters null character. strcat terminates when the concatenation process finishes.

strlen returns integer value. strcat returns pointer to the string.

General forms are as follows

int  strlen(const char*strng);

char *strcat(char *dest, const char *src);
strlen in cstrcat in c
Used to calculate the length of a string.Used to append source string to destination string.
int strlen(const char*strng);char *strcat(char *dest, const char *src);
Terminates when it encounters null characterTerminates when the concatenation process finishes.
Returns integer valueReturns pointer to a string.

Difference between strlen and strcmp in c

strlen in c is used to calculate the length of a string. strcmp function is used to compare two strings character by character.

strlen returns total length of a string. strcmp function returns 1.returns value<0 if st1 is less than st2. 2.it returns value>0 if st2 is less than st1. 3. it returns value=0 if boyh are equal.

strlen terminates when it encounters null character. strcmp terminates when comparision operation finishes.

strlen returns integer value. strcmp returns one of the three integer value(negative,positive,zero).

General forms are as follows

int  strlen(const char*strng);

int strcmp(const char*st1,const char*st2);
strlen in cstrcmp in c
Used to calculate the length of a string.Used to compare two strings character by character.
int strlen(const char*strng);int strcmp(const char*st1,const char*st2);
Returns total length of a string.Returns 1.returns value<0 if st1 is less than st2.
2.it returns value>0 if st2 is less than st1.
3. it returns value=0 if boyh are equal.
Terminates when it encounters null character.Terminates when comparision operation finishes.


Difference between strlen and strcpy in c

strlen in c is used to calculate the length of a string. strcpy is used to copy the source string to destination string.

strlen returns total length of a string. strcpy returns pointer to the destination string.

strlen terminates when it encounters null character. strcpy function terminates after process is over.

strlen returns integer value. strcpy returns pointer.

General forms are as follows

int  strlen(const char*strng);

char *strcpy(char *destn,const char*src);
strlen in cstrcpy in c
Used to calculate the length of a string.Used to copy the source string to destination string.
int strlen(const char*strng);char *strcpy(char *destn,const char*src);
Returns total length of a string.Returns pointer to the destination string.
Returns integer value.Returns pointer.

Difference between strlen and strchr in c

strlen in c is used to calculate the length of a string. strchr is used to search for the first occurrence of a character in the string pointed to by string argument.

strlen returns the total length of a string. strchr returns a pointer to the first occurrence of a character in a string otherwise NULL if not found.

strlen terminates when it encounters null character. strchr terminates when process is over.

strlen returns an integer value. strchr returns a pointer to the first occurrence of a character in a string otherwise NULL if not found.

General forms are as follows

int  strlen(const char*strng);

char *strchr(const char*string,int a);
strlen in cstrchr in c
Used to calculate the length of a string.Used to search for the first occurrence of a character in the string pointed to by string argument.
int strlen(const char*strng);char *strchr(const char*string,int a);
Returns total length of a string.Returns a pointer to the first occurrence of character in a string otherwise NULL if not found.
Terminates when it encounters null character.Terminates when process is over.

Difference between strlen and strrev in c

strrev in c is used to reverse a given string. strlen in c is used to calculate the length of a string.

strrev used to reverse a given string. strlen returns total length of a string.

strrev returns reversed string . strlen returns integer value.

General forms are as follows

char *strrev(char * strin);

int strlen(const char*strng);
strlen in cstrrev in c
used to calculate the length of a string.used to reverse a given string.

int strlen(const char*strng);
char *strrev(char * strin);
returns total length of a string.used to reverse a given string.

People also ask for

Does strlen counts spaces in c?

space is processed as another non-null character in c. strlen is used to find the total length of a string.

Does strlen counts null character?

No it doesn’t calculates null character. It terminates when it encounters null character.

What is sizeof in c?

Sizeof has nothing to do with strlen in c programming. It is a unary operator. It is used to find the total size of an operand. It can be used to find the total size of any type of variable.


People also read