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://codercto.com/courses/l/17.html
程序员修炼之道(影印版)
Andrew Hunt、David Thomas / 中国电力出版社 / 2003-8-1 / 39.00
本书直击编程陈地,穿过了软件开发中日益增长的规范和技术藩篱,对核心过程进行了审视——即根据需求,创建用户乐于接受的、可工作和易维护的代码。本书包含的内容从个人责任到职业发展,直至保持代码灵活和易于改编重用的架构技术。从本书中将学到防止软件变质、消除复制知识的陷阱、编写灵活、动态和易适应的代码、避免出现相同的设计、用契约、断言和异常对代码进行防护等内容。一起来看看 《程序员修炼之道(影印版)》 这本书的介绍吧!