“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

Recently, I want to review C language, so I will update an article about C language in nuggets every day! Freshmen who are just learning C, and those who want to review C /C++, don’t miss it! Solid foundation, slow down is fast!


5. Simulate strlen function in a non-recursive way

Strlen: library function that calculates the length of a string, referencing the string.h header file. Stop counting on \0.

Strlen and sizeof are a couple of things that are a little bit similar, but you can check out my previous article. Strlen and sizeof difference article link: C language -strlen and sizeof difference

Int my_strlen1(char* s) {int count = 0; // If (*s! = '\0') { s++; count++; } return count;Copy the code

6. Strlen function is simulated in a recursive way

The illustration


Int my_strlen2(char* s) {if (*s! = '\0') return 1 + my_strlen2(s + 1); else return 0; } int main() { char arr[] = "Mango"; int ret1 = strlen(arr); int ret2 = my_strlen1(arr); int ret3 = my_strlen2(arr); printf("%d %d %d", ret1, ret2, ret3); return 0; }Copy the code


7. Use non-recursion to reverse string order

Method: Use two Pointers, one to the left and one to the right. The character pointed to by the left pointer is swapped with the character pointed to by the right pointer. The loop condition is left < right


Void reverse(char* arr) {int len = strlen(arr); Char * right = arr + len-1; char* right = arr + len-1; char* left = arr; While (left < right) {char TMP = 0; tmp = *left; *left = *right; *right = tmp; left++; right--; } } int main() { char arr[] = "ognam"; reverse(arr); for (int i = 0; i < 5; i++) { printf("%c", arr[i]); } return 0; }Copy the code

Running results:


8. Use recursion to reverse string order

The illustration

If you don’t understand recursion, you can do what I did, and make up an example and use an expansion diagram to understand it!

Void reverse_string(char* arr) {int len = strlen(arr); char tmp = *arr; *arr = *(arr + len - 1); *(arr + len - 1) = '\0'; if (strlen(arr + 1) >= 2) reverse_string(arr + 1); *(arr + len - 1) = tmp; } int main() { char arr[] = "ognam"; reverse_string(arr); for (int i = 0; i < 5; i++) { printf("%c", arr[i]); } return 0; }Copy the code

9. Recursively calculate the sum of each digit of a number divided into each digit

The illustration


int DigitSum(int n)
{
	if (n)
		return n % 10 + DigitSum(n / 10);
	else
		return 0;
}
int main()
{
	int n = 0;
	scanf("%d", &n);
	int sum = DigitSum(n);
	printf("%d", sum);
	return 0;
}

Copy the code

Running results:


10. Use recursion to calculate n to the k

The illustration


Int my_pow(int n, int k) {// if (k == 0) {return 1; } else if (k >= 1) return my_pow(n, k - 1)*n; } int main() { int n = 0; int k = 0; scanf("%d %d", &n, &k); int ret1 = pow(n, k); int ret2 = my_pow(n, k); printf("%d %d", ret1, ret2); }Copy the code

Running results:


11. Find the NTH Fibonacci number using nonrecursion

The illustration


Int Fic(int n) {return n <= 2? 1 : Fic(n - 1) + Fic(n - 2); } int main() { int n = 0; scanf("%d", &n); int ret = Fic(n); printf("%d", ret); return 0; }Copy the code

This writing method needs to calculate a lot of repeated numbers, low efficiency!


Method 2: Iterative calculation


Int Fic(int n) {// int Fic(int n) { Will give a, b value will give cycle calculation/b/c value from a Fibonacci number 3 starts, computing, computing the fourth Fibonacci number to compute 2 times / / -- -- ", "so the first n to calculate n - 2 times int a = 1; // Fibonacci int b = 1; // Fibonacci int c = 0; while (n > 2) { c = a + b; a = b; b = c; n--; } return c; } int main() {int n = 0; scanf("%d", &n); int ret = Fic(n); printf("%d", ret); return 0; }Copy the code

Running results:


This is almost the end of recursion exercises, tomorrow, I will bring you two classic recursion problems: frog jump steps and Hannott tower problem! Welcome to continue to pay attention to!

That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!