How to implement the strlen function in C

1 Answer

0 votes
#include <stdio.h>

size_t strlen(const char* s) {
	// find length of s
	const char* p;

	for (p = s; *p != '\0'; p++)
		;
	return p - s;
}

int main(void) {
	char str[] = "c programming";

	printf("%zu", strlen(str));

	return 0;
}




/*
run:
    
13
    
*/

 



answered Dec 18, 2022 by avibootz

Related questions

3 answers 305 views
1 answer 125 views
125 views asked Sep 20, 2021 by avibootz
2 answers 290 views
1 answer 89 views
89 views asked Jun 10, 2025 by avibootz
...