您对在 C 中不使用 strlen() 或循环来打印字符串长度有何看法?这可能吗?

发布于 2025-01-09 04:02:37 字数 1437 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞别 2025-01-16 04:02:37

看来您需要的是以下内容

scanf( "%100s%n", str, &strlen );

请注意,使用名称 strlen 作为与标准字符串函数 strlen 对应的变量是一个坏主意。

It seems what you need is the following

scanf( "%100s%n", str, &strlen );

Pay attention to that using the name strlen for the variable that corresponds to the standard string function strlen is a bad idea.

智商已欠费 2025-01-16 04:02:37

另一种可能性(虽然不能完全填补你的空白)是:

#include <stdio.h>

int main()
{
    char str[101];
    int len;
    printf("enter a string: "); fflush(stdout);
    if(scanf("%100s", str) == 1) {
        printf("the length of the string \"");
        len = printf("%s", str);
        printf("\" is %d\n", len);
    }
}

Another possibility (though not quite exactly filling in your blanks) is:

#include <stdio.h>

int main()
{
    char str[101];
    int len;
    printf("enter a string: "); fflush(stdout);
    if(scanf("%100s", str) == 1) {
        printf("the length of the string \"");
        len = printf("%s", str);
        printf("\" is %d\n", len);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文