Grafitpan
Grafitpan

Reputation: 13

In C , what can I use instead of strlen if I don't want to use a string function?

I recently started learning basic C and I am still noobie with it, I started doing some projects for learning and I am using library functions but I am interested in other methods...

So I have an email validation, it works fine, but I want to do it without strlen, any suggestion what can I do instead of strlen?

void mail(char e[]) {
    int count = 0;
    int countb = 0;
    int i, j;
    int t, t2;
    int p = 0;

    for (i = 0; i < strlen(e); i++) {
        if (e[i] == '@') {
            count++;
            t = i;
        }
    }
    if (count == 1) {
        for (j = 0; j < t; j++) {
            if (!(e[j] == '_' || e[j] == '.' || isalpha(e[j]) || isdigit(e[j]))) {
                p = -1;
                printf("\nwrong\n");
                break;
            }
        }
        if (p == 0) {
            for (i = t; i < strlen(e); i++) {
                if (e[i] == '.') {
                    t2 = i;
                    countb++;
                }
            }
            if (countb == 1) {
                for (i = 0; i < t2 && i > t2; i++) {
                    if (!(isalpha(e[i]))) {
                        p = -1;
                        printf("\nwrong\n");
                        break;
                    } else {
                        p = 1;
                    }
                }
                if (p == 1) {
                    if (e[t2 + 3] != '\0') {
                        p = -1;
                        printf("\nwrong\n");
                    }
                }
            } else {
                p =- 1;
                printf("\nwrong\n");
            }
        }
    } else {
        p = -1;
        printf("\nwrong\n");
    }
    return;
}

Upvotes: 1

Views: 643

Answers (3)

The only thing you can do is to implement your own strlen function.

In C, strings are null-terminated char arrays, that means, the last character of an array is '\0' (zero). The strlen function iterates over the character array until a null-character is found, it returns the length of that array, the null-character not included.

To duplicate (create) a c-string, you have to do the following:

char *str = malloc(strlen(another_string) + 1);
str[strlen(another_string)] = '\0';

Example for strlen:

size_t my_strlen(const char *str)
{
    if (!str) return 0;
    size_t n = 0;
    for (; *str; ++n, ++str);
    return n;
}

Upvotes: 0

chqrlie
chqrlie

Reputation: 145277

Instead of comparing i < strlen(e) you can test if the byte at offset i is not the null terminator: e[i] != '\0'.

You can also compute the length of the string just once at the beginning of the function and store it into a len variable.

Upvotes: 5

UnDesSix
UnDesSix

Reputation: 68

You can create you own my_strlen :)

unsigned int      my_strlen(char *str)

OR

you can loop while your char is different of '\0' (null byte). So just replace strlen(e) with e[i] != '\0'

It should work just fine

Upvotes: -1

Related Questions