strlen() Function In C and C++ Tutorial with Examples – POFTUT

strlen() Function In C and C++ Tutorial with Examples


C and C++ programming languages provide the strlen() function in order to calculate or return the size of the given string or char array. String and char array is the same data type in C and C++ which simply a character array. strlen() function provided from the string.h header or library.

strlen() Function Syntax

strlen() function has the following syntax where we provide the char array or string as a parameter.

size_t strlen(const char *STR)
  • size_t is the return value type of the strlen() function which is generally integer or long which returns the size or length of the given string or char array.
  • const char *STR specifies the string or char array we want to get length or size.

String Length Of A String

We will start by calculating the length of the given string. Actually we will provide a char array where it will be presented in string format. We will provide the string named a and b with values My name is poftut.com and I love poftut.com. We will calculate these strings or sentences length with the strlen() function.

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100]="My name is poftut.com";

    char b[100]="I love poftut.com";
 
    a_length = strlen(a);

    b_length = strlen(b);
    
    printf("Length of string a = %ld \n",a_length);
    
    printf("Length of string b = %ld \n",b_length);
    
    return 0;
}

String Length Of A String with Char Pointer

Alternatively, we can create the string by using char pointer which will actually a char array or string too. We will provide the string named a and b with values My name is poftut.com and I love poftut.com. We will calculate these strings or sentences length with the strlen() function.

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100]="My name is poftut.com";

    char b[100]="I love poftut.com";

    printf("Length of string a = %ld \n",strlen(a));

    printf("Length of string b = %ld \n",strlen(b));

    return 0;
}

String Length Of A Char Array

Alternatively, we can create or define a char array item by item and then calculate the size of the complete char array. We will set the Hello poftut.com sentence characters character by character into char array a.

#include <stdio.h>
#include <string.h>

int main()
{
    char a[100]={'H','e','l','l','o',' ','p','o','f','t','u','t','.','c','o','m'};

    printf("Length of string a = %ld \n",strlen(a));

    return 0;
}

Length Of A Input String or Char Array

We can also get the char array or string from the input by using scanf(). We will read the input from the standard input or console and set into a char array then we will calculate the length with the strlen() function.

#include <stdio.h>
#include <string.h>

int main()
{
    char name[100];
    char sentence[100];


    printf("Please enter your name\n");
    scanf("%s",name);
    printf("Please enter a sentence\n");
    scanf("%s",sentence);
    printf("\n");


    printf("Length of name = %ld \n",strlen(name));
    printf("Length of sentence = %ld \n",strlen(sentence));

    return 0;
}
Length Of A Input String or Char Array
Length Of A Input String or Char Array

LEARN MORE  Php - String Variable Type

Leave a Comment