C program to find length of the string using pointer is a simple and easy program to find the length of the string using a pointer.

What is pointers in c?
The pointer is a variable that stores address of another variable.
Syntax for pointers in c
<Datatype> *pointername;
Example:- int *ptr;
Accessing the value of the variable:-
& Address of the operator gives the address of the variable.
* gives the value at that address stored in the pointer variable.

How to find the length of the string using a pointer
First, we will take a string input from the user. Take one counter then we copy the base address of the string to the pointer variable. Next, read the string from base pointer one by one and simultaneously increment the counter. String encounters end then stop. print the length of the string.

Algorithm to find length of the string using pointer

  1. Declare variable
  2. Take a string input
  3. Copy base address to a pointer variable.
  4. Traverse the string from base address
    Increment count value
    If string encounter end
    Stop
  5. Print length of the string.

Program to find length of the string using pointer

#include<stdio.h>
main()
{
char s[25],*t;
int len=0;
printf("Enter a string\n");
scanf("%s",&s);
t=s;//copying base address of string
while(*t!='
#include<stdio.h>
main()
{
char s[25],*t;
int len=0;
printf("Enter a string\n");
scanf("%s",&s);
t=s;//copying base address of string
while(*t!='\0')
{
len++;
t++;
}
printf("length of string is %d\n",len);
}
') { len++; t++; } printf("length of string is %d\n",len); }

Output :

Find length of the string using pointer

Write A Comment