Below is the code to find the length of a string using for loop.
#include <stdio.h>
int main(){
char str[1000];
printf("Enter a sentence: ");
fgets(str,1000,stdin);
int i,len=0;
for(i=0;str[i]!='\0';i++){
if (str[i] == '\n')
{
continue;
}
len++;
}
printf("Length of the string is %d",len);
}
Contents
Output:
Enter a sentence: Programming is soo interesting
Length of the string is 30