Length of String in C Language-Linux Hint

    A string in C language is an array of characters that’s terminated with a null character (). The string size is the variety of characters in a string. Within the string size ‘,’ a personality just isn’t counted.

    Within the instance proven above, the size of the string str is 6.

    On this tutorial, first, we’ll present how you can use a person outlined perform to calculate size of a string, after which we’ll present you a built-in library perform strlen(). We additionally present you the makes use of of the sizeof operator for string literals.

    String Size Utilizing Consumer Outlined Operate

    You possibly can write a person outlined perform which returns the variety of characters in a string.


    int stringLength(char *str)

    int i=0;

    whereas(str[i] != ”) i++;

    return i;

    int important()

    Right here, we iterate the whereas loop from i = Zero till we don’t encounter the ‘’ character. The worth of i is elevated by 1 in every iteration of the whereas loop. When the loop ends, the worth of i is the size of the string.

    String Size Utilizing Constructed-In Library Operate

    The built-in library perform strlen() will also be used to find out string size.

    strlen() perform:

    Header file:


    size_t strlen (const char *str)

    Argument: This perform takes an argument of the kind pointer to char.

    Return worth: This perform returns the size of the string str. Be aware that size_t is simply an alias of an unsigned integer.


    int important()

    char str1[30]=”STRING ARRAY”;
    char *str2;
    char *str3;

    str2 = str1;
    str3 = “STRING POINTER”;

    printf(“Size of the string str1 is => %ldn”,strlen(str1));
    printf(“Size of the string str2 is => %ldn”,strlen(str2));
    printf(“Size of the string str3 is => %ldn”,strlen(str3));
    printf(“Size of the string “STRING LITERALS” is => %ldn”,strlen(“STRING LITERALS”));

    return 0;

    Right here, we cross string array, string pointer, and string literal to the strlen perform, and the perform returns the size of the string.

    String Size Utilizing sizeof Operator

    We can also use the sizeof operator for string size (just for string literal). However, now we have to subtract 1 from the worth returned by this operator, as a result of it additionally counts the’’ character. For array and pointer, the sizeof operator returns the allotted measurement of the array and the pointer, respectively.


    int important()

    char str1[30] = “STRING”;
    char *str2 =str1;

    printf(“Size of “STRING” is => %dn”,(sizeof(“STRING”) – 1));
    printf(“Allotted measurement of the str1 array is => %dn”,sizeof(str1));
    printf(“Measurement of the str2 pointer is => %dn”,sizeof(str2));

    return 0;

    Right here, in Line no 9, we cross the string literal “STRING” and get the scale, together with the ‘’ character. So, we subtract 1 and get the precise measurement of the string.

    After we cross an array to the sizeof operator, it returns the allotted measurement of the array, which is 30, and when passing a personality pointer, it returns the scale of the pointer.


    So, on this tutorial, now we have proven you the way string size could be calculated in numerous methods. You need to use in your code whichever methodology is greatest fitted to you.

    awk command to find length of string,how to check length of field in unix,bash if string length greater than 0,bash repeat character n times,strlen function in unix,bash count characters in file,how to find length of string in c,how to find string length in c++,array length function in c,c program to concatenate two strings,how to find length of string in python,how to find length of string in java,c++ string length vs size,length of char array – c++,sizeof(string c++),c++ length of array,charat c++,unix word length,size of string unix,trim in unix,c programming input/output example,c++ normal program,in c programming,c++ programming exercises and solutions pdf,c programming exercises with solutions pdf,union elements can be of different sizes.,write a shell script to find the length of a given string.,length of a string in linux,c shell string length,linux number of characters in a string,linux grep string length,character length linux

    Recent Articles

    FedRAMP – What’s the Big Deal?

      In case you are somebody who works for a cloud service supplier within the enterprise of federal contracting, you in all probability have already...

    Planning a Game Night? Here Are 5 Of the Best Multiplayer Games You Can Enjoy With Friends – NoobsLab

      It has been months because the quarantine has began, and we perceive that cabin fever is getting the perfect of us. The perfect factor...

    Bash How to Execute a Command in a Variable? – Linux Hint

    Bash scripts will be created in a wide range of alternative ways and most of us are aware of executing the straightforward instructions inside...

    Things you should know about IP address

      You employ wifi for years however don’t know concerning the IP deal with and all the time should ask for assist every time...

    55 New security vulnerabilities reported in Software and Services for Apple

      A workforce of 5 safety researchers analyzed a number of Apple on-line providers for 3 months and located as many as 55 vulnerabilities, 11...

    Related Stories