I want to find if there is any way to find the length of any string in C
.
Here's how I did:
#include <stdio.h>
int main()
{
char s[10] = "hello";
int i , len = 0;
for(i = 0; s[i] != '\0'; i++)
{
len++
}
printf("length of string is: %d" , len);
return 0;
}
I want to find, if there is any way to get the length of string in just one line of code.
You can just simply do this:
for(len = 0; s[len] != '\0'; len++);
So in just one line of code you will get the length of string stored in len.
You can remove s[len] != '\0';
comparison to make it shorter:
for(len=0;s[len];len++);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With