strlen with Pointers

Lab C Intermediate 20 points

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
Sample input available Sample output available
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;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD