Created
September 14, 2017 08:00
-
-
Save longyue0521/87edfda04654ab6c22dc03b55e97c963 to your computer and use it in GitHub Desktop.
strlen() implementation in c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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