#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//用递归的方式实现字符串长度的输出
int StrLen(char* p)
{
if (*p == '\0')
{
return 0;
}
else
{
//指向下一个原字符的下一个
return 1 + StrLen(++p);
}
}
int main()
{
char str[] = "abckdf";
printf("%d\n", StrLen(str));
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//用非递归的方式实现字符串长度的输出
int StrLen(char* p)
{
int count = 0;
for (p; *p != '\0'; p++)
{
count++;
}
return count;
}
int main()
{
char str[] = "abckdf";
printf("%d\n", StrLen(str));
system("pause");
return 0;
}