Home >Backend Development >C#.Net Tutorial >[C language] Recursive and non-recursive implementations of strlen respectively

[C language] Recursive and non-recursive implementations of strlen respectively

little bottle
little bottleforward
2019-04-10 09:08:132490browse

今天带大家一起学习一下用递归和非递归分别实现strlen,对啦,这篇文章用的是C语言,这个大家应该会很熟悉吧,快来看看吧。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int Strlen1(char* str) {//递归
	if (*str == &#39;\0&#39;) {
		return 0;
	}
	else {
		return Strlen1(str + 1) + 1;
	}
}
//************
int Strlen2(char* str) {//非递归
	int n = 0;
	while (*str != &#39;\0&#39;) {
		++str;
		++n;
	}
	return n;
}
void main() {
	char str[30] = { 0 };
	printf("请输入一串字符\n");
	scanf("%s", &str);
	printf("递归判断字符串长度是:%d\n", Strlen1(str));
	printf("非递归判断字符串长度是:%d\n", Strlen2(str));
	system("pause");
}

【推荐课程:C视频教程

The above is the detailed content of [C language] Recursive and non-recursive implementations of strlen respectively. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete