Write a program to find the length of a string using pointers .
Examples:
Input : given_string = "geeksforgeeks" Output : length of the string = 13 Input : given_string = "coding" Output : length of the string = 6
Prerequisite : Pointer in C
Approach Used : In this program we make use of * operator. The * (asterisk) operator denotes the value of variable . The * operator at the time of declaration denotes that this is a pointer, otherwise it denotes the value of the memory location pointed by the pointer. In the below program, in string_length function we check if we reach the end of the string by checking for a null represented by ‘\0’ .
// C++ program to find length of string // using pointer arithmetic. #include <iostream> using namespace std; // function to find the length // of the string through pointers int string_length(char* given_string) { // variable to store the // length of the string int length = 0; while (*given_string != '\0') { length++; given_string++; } return length; } // Driver function int main() { // array to store the string char given_string[] = "geeksforgeeks"; cout << string_length(given_string); return 0; } |
Output:
13
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C++ Program to compare two string using pointers
- Check if a string is palindrome in C using pointers
- Program to Reverse a String using Pointers
- Program to reverse an array using pointers
- C program to sort an array using pointers
- Why C treats array parameters as pointers?
- What are Wild Pointers? How can we avoid?
- Introduction of Smart Pointers in C++ and It's Types
- Dangling, Void , Null and Wild Pointers
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Pointers in C/C++ with Examples
- Pointers vs References in C++
- What are near, far and huge pointers?
- Output of C programs | Set 64 (Pointers)
- Applications of Pointers in C/C++
- Difference between pointer to an array and array of pointers
- Features and Use of Pointers in C/C++
- Difference between Iterators and Pointers in C/C++ with Examples
- Program to implement Separate Chaining in C++ STL without the use of pointers
- Chain of Pointers in C with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

