脚本宝典收集整理的这篇文章主要介绍了字符串长度C语言版,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
题目描述:
在右侧我们给出了一个已经基本完成的程序,读入了一个字符串,调用了一个叫str_len的函数来计算这个字符串的长度,并输出。
聪明的你应该已经发现了,这个叫str_len的函数并没有完成,在不修改函数原型的情况下,请完成str_len函数,实现我们上述的功能吧。
样例输入
abcdefg
样例输出
7
解题源码:
/****************************
*版权所有(C)2017,Liu Hongyang。
*文件名称:zifucuan.c
*文件标识:无
*内容摘要:字符数目统计
*其他说明:无
*当前版本:V1.0
*作者: 刘红杨
*完成日期:201704
****************************/
#include<stdio.h>
#include<stdlib.h>
int @H_360_44@str_len(char *str);
int main()
{
char *str = (char *)malloc(100*sizeof(char));
scanf("%s",str);
printf("%d",str_len(str));
free(str);
return 0;
}
int str_len(char *str)
{
int i =0;
while(*(str+i)!=' ')
{
i++;
}
return i;
}
上题目是统计一串没有空格的字符串时,才能够准确得到数值。现在我将题目稍微延伸一下,统计带有空格的字符串中字符的个数:
比如I LIKE CODING