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
Release: 2019-04-10 09:08:13
forward
2498 people have browsed it

今天带大家一起学习一下用递归和非递归分别实现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");
}
Copy after login

【推荐课程: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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template