C 语言教程 C 练习实例70

naylor · 2022-04-04 13:18:15 · 热度: 5

题目:写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度。

程序分析:无。

实例

// Created by www.codercto.com on 15/11/9. // Copyright © 2015年 码农 教程. All rights reserved. // #include <stdio.h> #include <stdlib.h> int main() { int len; char str[20]; printf("请输入字符串:\n"); scanf("%s",str); len=length(str); printf("字符串有 %d 个字符。",len); } //求字符串长度 int length(char *s) { int i=0; while(*s!='\0') { i++; s++; } return i; }

以上程序执行输出结果为:

请输入字符串:
www.codercto.com
字符串有 14 个字符。

查看更多 C 语言经典100例

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册