Returns the length of a string.
Computes the length of the string str up to but not including the terminating null character.
Returns the number of characters in the string.
size_t strlen(const char *str );
This function has the following parameter.
The length of string.
#include <stdio.h>
#include <string.h>
/*from w ww .j a v a 2 s . c om*/
int main (){
char szInput[256];
printf ("Enter a sentence: ");
gets (szInput);
printf ("%u characters long.\n",(unsigned)strlen(szInput));
return 0;
}
The code above generates the following result.
strlen() function gives the length of a string in characters.
#include <stdio.h>
#include <string.h> /* provides strlen() prototype */
#define PRAISE "You are an extraordinary being."
int main(void) {
char name[40];
/* www . j a v a2 s . c o m*/
printf("What's your name? ");
scanf("%s", name);
printf("Hello, %s. %s\n", name, PRAISE);
printf("Your name of %zd letters occupies %zd memory cells.\n", strlen(name), sizeof name);
printf("%zd letters ", strlen(PRAISE));
printf("occupies %zd cells.\n", sizeof PRAISE);
return 0;
}
The code above generates the following result.
#include <string.h>
#include <stdio.h>
/* ww w .java2s . c o m*/
int main(void){
const char str[] = "this is a test";
printf("without null character: %zu\n", strlen(str));
printf("with null character: %zu\n", sizeof str);
printf("without null character: %zu\n", strnlen_s(str, sizeof str));
}
The code above generates the following result.