+ 2
What is the real use of strnlen() ??
4 Answers
+ 2
strnlen (s,n) limits scanning the end of string to n characters. It is useful for finding strings shorter than n. Also it is a memory safe implemetation of strlen, which can prevent looking for the trailing zero outside of allocated memory, so prevents possible application crash.
+ 4
Rewa Mathur Actually I missed the 'n' in strnlen! Lol
+ 3
A useful example would be looking through a buffer from beginning till the last non-null character Ā¹ for some particular characters. Extending/modifying the Rewa's example:
#include <stdio.h>
#include <ctype.h>
#define MAX 1000
int main()
{
char s[MAX];
size_t i, digit = 0;
printf("Enter a string: ");
fgets(s, MAX, stdin);
size_t length = strlen(s);
for (i = 0; i < length; ++i) {
if (isdigit(s[i]))
++digit;
}
printf("Number of digit(s) in s is: %zu", digit);
}
______
Ā¹ `strlen` searches for the first '\0' character
+ 2
Oh yeah?!
-1 for not giving credit.
So, you know how to Google and find your answer on Stackoverflow, but you don't know how to copy/paste the link for credit?
https://stackoverflow.com/questions/26349367/what-is-the-real-use-of-strnlen