C Program to Find the Length of a String
Last Updated :
08 Aug, 2024
Write a C program to find the length of a string.
The length of a string is the number of characters in it excluding the null character (‘\0’).
Examples:
Input: str[] = “Hello”
Output: 5
Explanation: The string “Hello” has 5 characters.
Input: str[] = “GeeksforGeeks”
Output: 13
Explanation: The string “GeeksforGeeks” has 13 characters.
Different Ways to Find the Length of a String in C
There can be many different ways to find the length of the string. Some of the popular methods are listed below:
1. Find the Length of the String Using a Loop
The idea is to initializes a variable length to zero. Then iterates through the string character by character, checking each character until it encounters the null terminator (‘\0’), which signifies the end of the string. For each character encountered, increments the length counter. Once the loop completes then returns the length which will give the length of the string.
- Create a variable length to 0.
- Use any loop to iterate through each character of the string until the null character (‘\0’) is encountered.
- Increment the length variable in each iteration.
- After the loop, the length variable will contain the actual length of the string.
C Program to Find the Length of the String Using a Loop
C
// C program to find the length of a string manually using a loop
#include <stdio.h>
int stringLen(char *str)
{
int length = 0;
// Loop till the NULL character is found
while (*str != '\0')
{
length++;
// Move to the next character
str++;
}
return length;
}
int main()
{
char str[1000] = "GeeksforGeeks";
int length = stringLen(str);
printf("Length of the String is %d", length);
return 0;
}
OutputLength of the String is 13
2. Find the Length of a String using strlen()
We can find the length of the string using the strlen() function. It is defined inside the strlib.h library.
Syntax of strlen()
int strlen(char* str);
C Program to Find the Length of a String using strlen()
C
// C program to find the length of
// string using strlen function
#include <stdio.h>
#include <string.h>
int main()
{
char str[1000] = "GeeksforGeeks";
// Calling strlen with Str as argument
int len = strlen(str);
printf("Length of the String is %d", len);
return 0;
}
OutputLength of the String is 13
3. Find the Length of a String Using Pointer Subtraction
We can use pointer subtraction technique to find the length of string. When we subtract the address of the first character from the address of the null terminator, the result tells us how many characters are in between, which equals the string’s length. Each character occupies one byte, so the difference directly represents the number of characters.
- Create a pointer to the first character of the string ptr = &str[0].
- Create a loop that runs till the pointer points to the null and in each iteration, increment the pointer.
- Use pointer subtraction to find the difference between the pointer at the end of the string and the pointer at the start (i.e, length = (ptr – &str[0])
- Return the difference as length.
C Program to Find the Length of a String Using Pointer Subtraction
C
// C program to find the size of string by pointer
// substraction
#include <stdio.h>
int stringLen(char *str)
{
// Start pointer pointing at the beginning
char *ptr = &str[0];
// Move the pointer until it points to null character
while (*ptr)
ptr++;
// Difference between the address will give the length
return ptr - str;
}
int main()
{
char str[] = "GeeksforGeeks";
//
int length = stringLen(str);
printf("Length of the String is %d\n", length);
return 0;
}
OutputLength of the String is 13
Frequently Asked Questions (FAQs) on Finding Length of String in C
Which method should I use to find the length of a string?
For simplicity and readability, using the strlen function is recommended. However, understanding the manual counting method and pointer subtraction can be useful for learning purposes.
Does the length include the null character (‘\0’)?
No, the length of a string is the number of characters excluding the null character (‘\0’).
Can we use the sizeof operator to find the size of a string?
The sizeof operator returns the total size of the memory allocated for an array, including any unused space, rather than the actual length of the string stored within it. This means that sizeof gives the size of the entire array (e.g., char str[100] would give 100), not the number of characters in the string.
Also, strings are sometimes represented as pointers and the sizeof operator will only return the size of that pointer.
Related Articles:
Please Login to comment...