第十六周 项目二 用指针玩转字符串(统计字符长度)

来源:互联网 发布:给游戏做美工需要什么 编辑:程序博客网 时间:2024/05/23 16:53
问题及代码:
/**Copyright (c) 2014,烟台大学计算机学院*All gight reserved.*文件名称:temp.cpp*作者:曾建强*完成时间:2014年12月12日*版本号:v1.0*问题描述:求字符串str的长度并返回*输入描述:*输出描述:*/#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)<<'\n';    cout<<pstrlen(s2)<<'\n';    cout<<pstrlen(s3)<<'\n';    return 0;}int pstrlen(char *str){    int i,j=0;    for(i=0;str[i]!='\0';i++)        j++;    return j;}

运行结果:


0 0
原创粉丝点击