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
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:
- Understanding C Pointers
- Using Pointers Add Numbers
- Average of Array With Pointer
- Sum of Array Using Pointers
- Using Pointers With Array Input Output