Skip to content
Home » C Programs » C Program to Find the Length of the String Using Pointers

C Program to Find the Length of the String Using Pointers

Basic c program to find the length of the string using pointers. In this program we can check the length of the string using pointers in c programming language for beginners.

String Definition in C

In C, a string sometimes referred to as a C string is an array of characters followed by a NULL character. A set of characters is contained within double quotations to represent a string (“). 

A string is a collection of characters encircled by double quotation marks. A string is a data type similar to an integer and a floating point unit that is used to represent text rather than numbers in programming. It is made up of a series of characters that may include spaces and numerals.

Pointers in C

A pointer is a variable whose value is the address of another variable, i.e. the memory location’s direct address. A pointer, like any other variable or constant, must be declared before it may be used to store any variable address. A variable that stores the address of another variable is known as a pointer. In this program ew have used the pointer to execute the c program to find the length of the string using pointers

C Program to Find the Length of the String Using Pointers

//c program to find the length of the string using pointers
#include<stdio.h>
int str_len(char*);
void main() {
char str[20];
int length;
printf("\nEnter the string");
gets(str);
length = str_len(str);
printf("The Length of the String %s is %d", str, length);
getch();
}
int str_len(char*p) 
{
int count = 0;
while (*p != '\0') {
count++;
p++;
}
return count;
}

Output:

Enter the stringinlarn.com
The Length of the String inlarn.com is 10