Talking about the non-recursive and recursive array reverse order

Talking about the non-recursive and recursive array reverse order

Non-recursive

#define N 10
#include <stdio.h>

void ReverseArr(int* a, int n)
{
	for (int i = 0; i <= n / 2; i++)
	{
		int tmp;
		tmp = a[i];
		a[i] = a[n - i - 1];
		a[n - i - 1] = tmp;
	}
}

int main()
{
	int arr[N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	ReverseArr(arr, N);
	for (int i = 0; i < N; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

  Array initialization needs to specify the size of the array and cannot be written as a variable. Therefore, a macro is used here for definition. Then, if you need to exchange, you only need to use a for loop to exchange the first value and the last value in the array. The non-recursive form is relatively simple, so I won't go into too much here.

Print output recursively

#include <stdio.h>

void reverse_string(char* string)
{
	if (*string != 0)
	{
		reverse_string(string + 1);
	}
	//减去\0
	string--;
	printf("%c", *string);
}

int main()
{
	char arr[] = "student";
	int n;
	reverse_string(arr);

	return 0;
}

  In this part, we use the recursive method to find the element of the last array and print it out directly. Here we just print the array in reverse order, but in fact it does not change the order of the elements of the array. The logic is relatively simple, but in practice It is not recommended, because in the actual development process, the function call is usually to modify or get a return value, and then the return value is judged in the main function, or the value is directly modified, and then the modified value is It may become the value of another function and then be called. Therefore, if it is only for printing in reverse order, it can be used. In other cases, it is not recommended. For better recursive methods, please refer to the next one.

Recursively store the array in reverse order and print

#include <stdio.h>
#include <string.h>

int my_string(char* a)
{
	if (*a == '\0')
	{
		return 0;
	}
	else
	{
		return 1 + my_string(a + 1);
	}
}

void ReverseOrder(char a[])
{
	char temp = a[0];
	int n = my_string(a);
	a[0] = a[n - 1];
	a[n - 1] = '\0';
	int m = my_string(a + 1);
	if (m > 1)
	{
		my_string(a + 1);
	}
	a[n - 1] = temp;
}

int main()
{
	char arr[10] = { 0 };
	scanf("%s", arr);
	ReverseOrder(arr);
	printf("%s", arr);

	return 0;
}

This method is a great improvement over the previous method. It uses the recursive method to directly change the order of the array in the original array, and then save it. This is in the main function, if you want to use the reverse order The latter array is also possible, which is a once and for all method, but the recursive method of the array is logically more complicated than the non-recursive method. Therefore, in actual situations, it is recommended to use the non-recursive, but recursive method. It also needs to be mastered. It may still appear in the interview (you don't need to, but you must be able to write). The writing idea of ​​the summary code is as follows:

1. Save the first character
2. Replace the first letter with the
last character 3. The last character becomes \0
4. Recursive call
  4.1 Determine the condition
  4.2 Passing the parameter is to pass the string starting with the second character of the current string
5. Save with The first letter down, replace the current last character

Guess you like

Origin blog.csdn.net/weixin_43580319/article/details/111462130