字符串长度C语言版

发布时间:2019-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了字符串长度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

/****************************
 *版权所有(C)2017,Liu Hongyang。
 *文件名称:zifucuan.c
 *文件标识:无
 *内容摘要:字符数目统计
 *其他说明:无
 *当前版本:V2.0
 *作者:   刘红杨
 *完成日期:201704
 ****************************/


#include<stdio.h>
#include<stdlib.h>



int main()
{
    
    char str[20]= "i like you";
    printf("%d",str_len(20,str[20]));
    return 0;
}
int str_len(int length,char str[20])
{
    int temp,jishu=0;
    for(temp=1;temp<length;temp++)
    {
        if( *(str+temp)!='')
        {
            jishu++;
        }
        continue;
    }
    return jishu;
}

脚本宝典总结

以上是脚本宝典为你收集整理的字符串长度C语言版全部内容,希望文章能够帮你解决字符串长度C语言版所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。