strlen( ) in C

Example

#include <stdio.h>

int main() {
	char arr[] = "Bamboozled";
	int len1, len2;
	len1 = strlen(arr);
	len2 = strlen("Humpty Dumpty");
	printf("\nstring = %s length = %d", arr, len1);
	printf("\nstring = %s length = %d", "Humpty Dumpty", len2);
	getchar();
	return 0;
}

Let’s write an equivalent of strlen function as :

Example

#include <stdio.h>

int main() {
	char arr[] = "Bamboozled";
	int len1, len2;
	len1 = xstrlen(arr);
	len2 = xstrlen("Humpty Dumpty");
	printf("\nstring = %s length = %d", arr, len1);
	printf("\nstring = %s length = %d", "Humpty Dumpty", len2);
	getchar();
	return 0;
}
xstrlen(char *s) {
	int length = 0;
	while (*s != '\0') {
		length++;
		s++;
	}
	return (length);
}