1. 编程学习网 > 编程教程 > C语言教程 > C语言经典案例:写一个函数,求一个字符串的长度,在main函数中

C语言经典案例:写一个函数,求一个字符串的长度,在main函数中

C语言案例分析:无。

实现代码如下:

#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.runoob.com

字符串有 14 个字符。

本文由IT教学网整理发布,转载请注明出处:http://www.itjx.com/jiaocheng/cyuyan/885.html

联系我们

在线咨询:点击这里给我发消息

咨询电话:400-998-2681

工作时间:7*24小时无休