举例来说char buf[] = { 'a', 'b', 'c', 'd', 'e', 'f', '\0', 'x', 'y', 'z' };
字符串buf[],当输入N=10,或者20时输出6,N=3或者5时输出3或者5;
下面是具体算法
#include<iostream>
using namespace std;
int mystrlen(char *buf, int N)
{
if (N == 0) return 0;
else
{
if (buf[0] == '\0')return 0;
else return 1+ mystrlen(buf + 1, N - 1);
}
}
int main()
{
cout << "Please Input N:" << endl<<"N: ";
int N;
cin >> N;
char buf[] = { 'a', 'b', 'c', 'd', 'e', 'f', '\0', 'x', 'y', 'z' };
cout<<"Number Is : "<<mystrlen(buf, N);
system("pause");
return 0;
}