In this article, we will write a C program to find the length of a string using pointers.
The program takes a string from the user as input and prints its length on the screen as output.
Sample Input:
Enter any string: Hello World
Output:
The length of the string is: 11
In C programming language, a string is an array of characters terminated with a null character('\0'
). So, to get the length of the string we can count the total number of characters that the string contains.
There are different ways to get the length of a string in C. One of those ways is to use C pointers.
In C, a pointer is used to store the memory address of a variable. When we assign a string to a pointer, it by default stores the memory address of the first character of the string.
This means if we need to count all the characters of the string, we can increment the pointer by one in a loop and terminate it as soon as we reach the end of the string i.e. the null character '\0'
.
See implementation in the following C program:
// C program to get the length of a string using pointers #include <stdio.h> int main() { char str[100], * ptr; int count; printf("Enter any string: "); gets(str); // ptr pointing to first char of string ptr = str; // Initialize count to zero count = 0; // Run until null character is reached while ( *ptr != '\0') { count++; ptr++; } printf("The length of the string is: %d", count); return 0; }
Output:
Enter any string: Hello World The length of the string is: 11
Code Explanation:
- The program asks the user to enter a string which is stored in the
str
array. - To get the string from the user we have used the
gets()
function instead of thescanf()
function. This is because the scanf() function stops reading the string if it encounters a white space. - To count the characters in the string, we have created a pointer
*ptr
of type char and initialized it to our string i.e.ptr = str
. After initialization, the pointer*ptr
starts pointing to the first character of the string. - The
while
loop runs until it reaches the last character('\0'
) of the string. - In each iteration of the
while
loop, the pointer*ptr
increments by one i.e. it starts pointing to the next character of the string. The value of thecount
also increments by one in each iteration. - Finally, the length of the string is printed using the printf() function.
Using a Function with Pointer to Get the Length of the String
We can also create a user-defined function that counts the total number of characters in the string and returns it.
The function takes the string as an argument and returns the total number of characters it has:
// C program to get the length of a string using pointers #include <stdio.h> // Function declaration int countChars(char * ); int main() { char str[100]; int length; printf("Enter any string: "); gets(str); // Call the countChars() function length = countChars(str); // Print the length printf("The length of the string is: %d", length); return 0; } // Function to get string length int countChars(char * ptr) { int count = 0; while ( *ptr != '\0') { count++; ptr++; } return count; }
Output:
Enter any string: Hii there The length of the string is: 9
Thanks for reading.