In this article we are going to write a C program that take a String as input and calculate the length of String.
Table of Contents
C Program to Find the Length of a String
#include <stdio.h>
int stringLength(const char *str)
{
int length = 0;
while (str[length] != '\n')
{
length++;
}
return length;
}
void main()
{
char inputstring[100];
printf("Enter the String ");
fgets(inputstring, sizeof(inputstring), stdin);
printf("length of String = %d ", stringLength(inputstring));
}