90. Implement Custom strlen Function

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

int my_strlen(const char *str) {
    // Your logic here
    int j = 0;
    while(*(str+j))
    {
        j++;
    }
    return j;
}

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", my_strlen(str));
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote