C String Length Strlen Function

Syntax of the strlen Function

The strlen function has the following syntax:

Example

strlen(const char* s);

In this particular format,

  • S represents the specified string that requires length calculation.
  • Output Type: The strlen function yields the numeric length of the provided string.
  • Simple C string length Example

In this basic example, we will determine the size of a provided character array by utilizing the strlen function within the C programming language.

Example

Example

#include<stdio.h>  

#include <string.h>    

int main(){    // main function

char ch[20]={'T', 'p', 'o', 'i', 'n', 't', 't', 'e', 'c', 'h', '\0'};   

   printf("Length of string is: %d",strlen(ch));    

 return 0;    

}

Output:

Output

Length of string is: 10

Explanation:

In the provided code snippet, a character array of length 20 is initialized to store multiple characters along with the null terminator '\0'. The length of this character array is then determined using the strlen function. The resulting output displays a string length of 10, demonstrating that strlen calculates the number of characters in the array until it encounters the null terminator, excluding the null terminator itself.

C strlen Function Example to Count String Length

Let's consider an illustration to showcase how we can determine the size of a provided string by utilizing the strlen function in the C programming language.

Example

Example

#include <stdio.h>

#include <string.h>



int main()   // main function

{

    // defined a string 's'

    char s[] = "Logic Practice";

    // Counting the length of the given string 's'

    int len = strlen(s);

// Printing the length

    printf("The length of the given string s is : %d", len );

    return 0;

}

Output:

Output

The length of the given string s is: 10

Explanation:

In the provided code snippet, a variable 's' of character type has been initialized with a string value. Subsequently, an integer variable has been declared to store the length of the string 's'. Following this, the program displays the length of the string. The output reveals that the length is determined to be 10, indicating that the calculation was accurate.

C String strlen function Example with User Input

Let's explore an illustration to showcase the length of a string based on user input utilizing the strlen function in the C programming language.

Example

Example

#include <stdio.h>

#include <string.h>



int main() {   // main function

    char st[50];

    printf("Enter the String: ");

    fgets(st, sizeof(st), stdin);



    printf("Length of input String: %lu", strlen(st));

    return 0;

}

Output:

Output

Enter the String: Hello! This is C strlen() Function

Length of input String: 35

Explanation:

In this instance, a user input string is obtained by employing the fgets function, ensuring secure storage within the character array st. Subsequently, the strlen function is applied to determine the overall character count within the input string. Ultimately, the length calculated is displayed utilizing the printf function.

C String strlen Function Example With Spaces

Here, we will illustrate an example to showcase how to determine the length of a string including spaces by utilizing the strlen function in the C programming language.

Example

Example

#include <stdio.h>

#include <string.h>  // using header file for string



int main() {  // main function

    char st[] = "Hello! This is a C Programming Language.";

    printf("Length of the string is: %lu", strlen(st));

    return 0;

}

Output:

Output

Length of the string is: 40

Explanation:

In this instance, a variable called st is defined to hold a line of text. Subsequently, the strlen function is employed to calculate the number of characters within the string up to the null character \0, excluding it. Finally, the script outputs the total count of characters existing in the string.

Features of the String strlen Function in C

Several features of the string strlen function in C are as follows:

  • The string strlen function in C is utilized to return the length of a string by finding the length of characters until the null terminator, excluding it.
  • We cannot use this function to modify the original string. We can only read characters to find the length of the string.
  • It is commonly utilized in string manipulation operations, including concatenation, copying, validation, and many more.
  • It is a very simple and efficient method, which makes it one of the most utilized standard library string functions in C.
  • The strlen function can't detect buffer overflows. Therefore, the string must be null-terminated in the C program.
  • Conclusion

In summary, the strlen function in the C programming language offers a straightforward and effective approach to determine the length of a null-terminated string. This function aids in counting the characters within a string until reaching the terminating '\0' character. It facilitates various tasks like memory allocation, validation, iteration, string manipulation, and more. Ultimately, strlen is a crucial tool extensively employed in C programming to manage strings securely and precisely.

C strlen Function FAQs

1) Is the strlen function thread-safe in C?

Yes, the strlen function is considered thread-safe since it solely reads the string without altering the global state. Nevertheless, if another thread concurrently modifies the string, it can result in race conditions, causing unpredictable behavior.

Yes, it is achievable to manually create the functionality of the strlen method in the C programming language.

Yes, it is entirely feasible to create the strlen function from scratch in C. This function goes through each character in the array sequentially until it encounters the null terminator '\0', keeping track of the character count along the way.

When NULL is passed to the strlen function in C, it will result in undefined behavior. This is because the strlen function is designed to calculate the length of a string up to the null-terminating character '\0'. When NULL is passed, it points to an invalid memory location and trying to access the memory at that location can lead to unpredictable outcomes. It is important to always ensure that valid memory addresses are passed to functions to avoid such issues.

When NULL is provided to the strlen function, it leads to undefined behavior, typically resulting in a segmentation fault or program crash. Hence, it is crucial to verify for null values prior to invoking this function.

4) Is it possible to apply the strlen function to an uninitialized character array in the C programming language?

No, utilizing the strlen function on an uninitialized or garbage-value array may result in undefined behavior.

5) Is the strlen function time-efficient in C?

In C programming, the time complexity of the strlen function is O(n) as it necessitates scanning through each character until encountering the null-terminator.

Input Required

This code uses input(). Please provide values below: