C program to find length of the string using pointer is simple and easy Program to find length of the string using pointer.
In this tutorial we are going to see what is pointers, how to find length of the string using pointer and program to find length of the string using pointer.
First we will see pointers in c
What is pointers in c
Pointer is a variable which stores address of another variable.
Syntax for pointers in c
Example :- int *ptr;
Accessing value of variable:-
& Address of operator gives the address of variable.
* gives the value at that address stored in pointer variable.
How to find length of the string using pointer
First we will take a string input from user.Take one counter then we copy the base address of the string to pointer variable. Next read the string from base pointer one by one and simultaneously increment counter. String encounters end then stop. print the length of the string.
Concept you need to know from c
1. Basic input and output in c
2. Pointers in c
3. String in c
Algorithm to find length of the string using pointer
- Declare variable
- Take a string input
- Copy base address to pointer variable.
- Read the string from base address and increment count value
- if string encounter end then stop
- Print length of string.
Program to find length of the string using pointer
printf("Enter a string\n");
t=s;//copying base address of string
printf("length of string is %d\n",len);
1. Write header file.
2. Start with main function.
3. Declare variable
s => to store string
t => to store base address
len => to store length
5. Take string input
6. while(*t!=’\0′) to check string encounters ed otherwise increment len
7. Print length of string.
printf(“length of string is %d\n”,len);