Using Pointers Find String Length

By | December 9, 2022

Using Pointers Find String Length – Write a C Program to Find the length of the string using a pointer.

C Program to Find the length of the string using a pointer

Using Pointers Find String Length

Source Code Calculate Length of a String – C Pointers

#include <stdio.h>
 
int main() 
{
   char string1[50];
   char *ptr;
   int length=0;
   printf("\n\n Using Pointers : Find String Length:\n"); 
   printf("---------------------------------------------------\n");
   
   printf(" Enter a string :");
   gets(string1);
   
   ptr = string1; /*store base address of string1 into ptr */
   while (*ptr != '\0') 
   {
      length++;
      ptr++;
   }
   
   printf("\n Length of the string is: %d",length);
   printf("\n");
   
   return 0;
}
 

Output of Program

Using Pointers : Find String Length:
—————————————————
Enter a string :Using Pointers

Length of the string is: 14

You will also like to Read More on C Pointers Programming Examples:

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *