strlen with Pointers
Completed
Write int my_strlen(char *s) that walks a pointer to the '\0'. Print the length of each input word.
Input
n, then n words.
Output
n lines.
Example
Input
3 hello a programming
Output
5 1 11
Editor
C
Hints
Subtracting two pointers into the same array gives the number of elements between them.
Solution explanation
This is essentially how the real strlen is implemented.
Show solution
#include <stdio.h>
int my_strlen(char *s) {
char *p = s;
while (*p != '\0') p++;
return p - s;
}
int main() {
int n;
char w[100];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", w);
printf("%d\n", my_strlen(w));
}
return 0;
}