C++——写一函数,求一个字符串的长度。在main函数中输人字符串,并输出其长度。用指针或引用方法处理。

没注释的源代码

#include <iostream>

using namespace std;
int length(char *p);
int main()
{
    int len;
    char str[100];
    cout<<"please input string:";
    cin>>str;
    len=length(str);
    cout<<"the length of string is :"<<len<<endl;
    return 0;
}
int length(char *p)
{
    int n=0;
    while(*p!='\0')
    {
        n++;
        p++;
    }
    return n;
}
 

猜你喜欢

转载自blog.csdn.net/2303_80770781/article/details/143268132