第16周项目2-用指针对字符串进行操作(用数组作形参计算字符串长度)

来源:互联网 发布:最大子数组和 动态规划 编辑:程序博客网 时间:2024/06/03 05:41
/  *copyright (c)2014,烟台大学计算机学院  *All rights reserved  *文件名称:123.cpp  *作者:孙春红  *完成日期:2014年12月14日  *版本号:v1.0  * * 问题描述:编写程序,计算字符的长度。*输入描述:略。*程序输出:略。*/#include <iostream>#include <cstdio>using namespace std;int astrlen (char str[]);int main(){    char s1[100]={"I am a shy person"};    astrlen(s1);    cout<<"字符串是:"<<endl;    for (int i=0;i<100;i++)    {        cout<<s1[i];    }    cout<<endl;    cout<<"字符串的长度是:"<<endl;    cout<<astrlen(s1)<<endl;    return 0;}int astrlen(char str[]){    int i=0,j=0;    while (str[i]!='\0')    {        j++;        i++;    }    str[i]='\0';    return j;}

运行结果:

学习心得:

与指针作形参的求法差不多。

0 0