C 语言实例 - 计算字符串长度

C 语言教程 · 2019-02-21 09:12:27

计算字符串长度。

实例 - 使用 strlen()

#include <stdio.h> #include <string.h> int main() { char s[1000]; int len; printf("输入字符串: "); scanf("%s", s); len = strlen(s); printf("字符串长度: %d", len); return 0; }

输出结果为:

输入字符串: codercto
字符串长度: 6

实例 - 不使用 strlen()

#include <stdio.h> int main() { char s[1000], i; printf("输入字符串: "); scanf("%s", s); for(i = 0; s[i] != '\0'; ++i); printf("字符串长度: %d", i); return 0; }

输出结果为:

输入字符串: codercto
字符串长度: 6

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

Twisted Network Programming Essentials

Twisted Network Programming Essentials

Abe Fettig / O'Reilly Media, Inc. / 2005-10-20 / USD 29.95

Developing With Python's Event-driven Framework一起来看看 《Twisted Network Programming Essentials》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换