递归和非递归分别实现strlen。

递归

#include<stdio.h>//递归
int my_strlen(char *str)
{
	if (*str != '\0')
	{
		return 1 + my_strlen(str + 1);
	}
	else
		return 0;
}
int main()
{
	char *p = "abcdef";
	printf("%d\n", my_strlen(p));
	system("pause");
	return 0;
}

非递归

#include<stdio.h>//非递归
int my_strlen(char *str)
{
	int count = 0;
	while (*str != '\0')
	{
		count++;
		str++;
	}
	return count;
}
int main()
{
	char *p = "abcdef";
	printf("%d\n", my_strlen(p));
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43857558/article/details/84960039