All submissions

Implement Custom strlen Function

Code

#include <stdio.h>
#define MAXBUF 100

int custom_strlen(const char *str) {
    // Your logic here
    int i;
    for (i=0; (i < MAXBUF ); i++) {
        if (str[i] == '\0')
            return i;
    }
    return 0;
}

int main() {
    char str[101];
    fgets(str, sizeof(str), stdin);

    // Remove newline if present
    int i = 0;
    while (str[i] != '\0') {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
        i++;
    }

    printf("%d", custom_strlen(str));
    return 0;
}

Solving Approach

 

initialize a counter and run it to the maximum allowed (guaranteed < 100) and

compare the current character to '0'. once hit, the current value of i is the length

 

Loading...

Input

Embedded

Expected Output

8