第16周项目2.3 求字符串str的长度并返回 用指针作形参

来源:互联网 发布:26岁初抗老护肤 知乎 编辑:程序博客网 时间:2024/05/16 12:04
/*      *Copyright (c)2014,烟台大学计算机与控制工程学院      *All rights reserved.      *文件名称:gcd.cpp      *作    者:惠睿      *完成日期:2014年12月11日      *版 本 号:v1.0      *      *问题描述:用指针作形参求出字符串的长度。*程序输出:输出字符串的长度。*/ #include <iostream>using namespace std;int pstrlen(char *str);int main(){    char s1[50]="Hello world. ";    char s2[50]="Good morning. ";    char s3[50]="vegetable bird! ";    cout<<"字符串的长度为:"<< pstrlen(s1)<<"   "<<pstrlen(s2)<<"   "<< pstrlen(s3)<<endl;}int pstrlen(char *str){    int n;    n=0;    while(*str!='\0')    {        n++;        str++;    }    return(n);}

运行结果:

知识点总结:熟悉运用指针作形参。

0 0
原创粉丝点击