In this article, you will learn to find the length of a string using the strlen() and your own created functions. But before learning the code, I want to explain the meaning of the string in the C programming language.
The C string is an array of characters that is terminated with a null character '\0'
. That means null-character('\0')
is indicate the end of the C string. For example,
You can see the above-mentioned image, where “Aticleworld” is a C string. The length of the string is determined by counting all the characters excluding the terminating null character.
The primary prerequisites to understanding this C Program:
I will describe the three ways here to find the string length. First, I will describe how to find the length using the strlen function because it is the best way to get the string length. After that, you will learn to find the string length using the own created function. And in the last, I will describe how you can use the sizeof operator to find the length of character string literals.
String Length Using strlen():
The strlen function in C is a built-in library function defined in <string.h>
. It takes a string as an argument; computes the length and returns the length. It doesn’t count the null character (‘\0’
).
Syntax:
size_t strlen(const char *s);
Let us take a look at its implementation.
/* Find the length of a string using the strlen() function. */ #include <stdio.h> #include <string.h> int main() { // initializing the array to store string characters char str[] = "Aticleworld"; /* strlen() function return the length of the string and assign it to the length variable */ const size_t length = strlen(str); // printing the length of string printf("Length of the string is %d\n", length); return 0; }
Output:
Length of the string is 11
String Length in C using a User-Defined Function:
You can also find the length of a string using a user-defined function. You need to iterate over the string in a loop and increment a counter for each interaction till it reaches the null character ‘\0
‘.
Let’s see the below implementation:
/* Find the length of a string using the own created function function. */ #include <stdio.h> unsigned int getLen(const char *s) { unsigned int count = 0; if(s != NULL) { while(*s!='\0') { // incrementing the count till not // reach at termination null-character count++; s++; } } //return count of character return count; } int main() { // initializing the array to store string characters char str[] = "Aticleworld"; /* getLen() function return the length of the string and assign it to the length variable */ const unsigned int length = getLen(str); // printing the length of string printf("Length of the string is %d\n", length); return 0; }
Output:
Length of the string is 11
Character string literal length in C by using the sizeof() Operator:
A character string literal is a sequence of zero or more multibyte characters enclosed in double quotes. Using the sizeof() operator in C you can also find the length of the char string literal.
For Example: "Amlendra"
, it is a string literal because it is a sequence of a character and enclosing in the double quotes.
The sizeof
operator yields the size of its operand in bytes.
Let’s see the below implementation code.
/* Find the length of a string using the sizeof operator. */ #include <stdio.h> int main() { // initializing the array to store string characters char str[] = "Aticleworld"; /* sizeof() operator return the length of the string including null-char and assign it to the length variable */ unsigned int length = sizeof(str); //deduct 1 to remove null-char length -=1; // printing the length of string printf("Length of the string is %d\n", length); return 0; }
Output:
Length of the string is 11
Recommended Articles for you:
- Best Gifts for the programmer and techies.
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- Implement and use of memset of in C
- Use and create strcspn in programming.
- Use of memchr in C programming.
- strrchr in C programming.
- strchr in C programming.
- How to Use strncpy() and implement own strncpy().
- What is the difference between char array and char pointer in C?
- C identifiers and naming rules.
- Stringizing operator (#) in C
- Token Pasting Operator in C/C++ programming.