在C中,如果我不想使用字符串函数,我可以用什么来代替strlen?

我最近开始学习基本的 C 语言,但我仍然不熟悉它,我开始做一些学习项目,我正在使用库函数,但我对其他方法感兴趣......

所以我有一个电子邮件验证,它工作正常,但我想在没有strlen,任何建议我可以做什么而不是做什么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("nwrongn");
                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("nwrongn");
                        break;
                    } else {
                        p = 1;
                    }
                }
                if (p == 1) {
                    if (e[t2 + 3] != '') {
                        p = -1;
                        printf("nwrongn");
                    }
                }
            } else {
                p =- 1;
                printf("nwrongn");
            }
        }
    } else {
        p = -1;
        printf("nwrongn");
    }
    return;
}

回答

相反的比较i < strlen(e),你可以测试是否在偏移字节i不是空终止:e[i] != ''

您还可以在函数开始时仅计算一次字符串的长度并将其存储到len变量中。


以上是在C中,如果我不想使用字符串函数,我可以用什么来代替strlen?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>