个人答案:
#include <iostream>
using namespace std;
int stringlen(char* s);
int main()
{
char str[20];//给予字符串长度20
cout << "Please enter string: ";
cin >> str;
cout << "String length: " << stringlen(str) << endl;
system("pause");
return 0;
}
int stringlen(char* s)
{
char* p;
int i;
for (p = s, i = 0; *p != '\0'; p++, i++);//进行长度计算
return i;
}
结果: