Skip to content

Instantly share code, notes, and snippets.

@longyue0521
Created September 14, 2017 08:00
Show Gist options
  • Select an option

  • Save longyue0521/87edfda04654ab6c22dc03b55e97c963 to your computer and use it in GitHub Desktop.

Select an option

Save longyue0521/87edfda04654ab6c22dc03b55e97c963 to your computer and use it in GitHub Desktop.
strlen() implementation in c
#include <stddef.h>
/**
* return length of valid string which has '\0' or 0 as terminated
*/
size_t
str_len( const char *str)
{
size_t i;
for( i = 0; str[i] != '\0'; i++);
return i;
}
#include <stddef.h>
/**
* return length of valid string which has '\0' or 0 as terminated
*/
size_t
str_len( const char *str)
{
const char *p = str;
// while(*p != '\0') <=> while(*p != 0) <=> while(*p)
for(p = str; *p; p++);
return (p - str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment