Write a C Program to find the Length of a String
![Write a C Program to find the Length of a String Length of a String](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd7JUeFcGURR9T3358Yzicn60HdqsMSKqXUO3-Sxfx4K5Sqc7UgUwbzWzXCCtqRS4lZ_KrO_J8XbTe1fxRFzDQ-ZO_BYWAYQLuUNCKozi7hAbEpi0FGxITpJhJtEywftgs9V5CX1CA6O8a/s1600/c-programming-tutorial.jpg)
#include<stdio.h>
void main()
{
char str[100];
int i;
printf("Please enter a string: ");
// gets(str);
// fgets is a better option over gets to read multiword string .
fgets(str, 100, stdin);
// Following can be added for extra precaution for '\n' character
// if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL;
for(i=0;str[i]!=NULL;i++);
i=i-1;
printf("Length of string is: %d",i);
}
Comments
Post a Comment