C 练习实例70

C语言教程评论

C 练习实例70

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

程序分析:无。文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili70.html

#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;  
}

以上程序执行输出结果为:文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili70.html

请输入字符串:
www.nowcoder.com
字符串有 16 个字符。
文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili70.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili70.html
运营不易,
感谢支持!
weinxin
我的微信
我的微信公众号
我的微信公众号扫一扫
weinxin
我的公众号
 最后更新:2021-12-7
公式库网
  • 本文由 公式库网 发表于 2021年12月6日20:57:38
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-lianxishili70.html

发表评论