Write a C Program to find the Length of a String



Length of a String


#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

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C