C language basics - string reverse order (recursive implementation, without using library functions)

For this question, we have to pay special attention to: the characters in the string are arranged in reverse order, not printed in reverse order !

Below I will introduce this question with non-recursive and recursive methods respectively.

Start with the main() function

  1. Define an array.
  2. Call the function that implements the reverse order of the string. ——The core of this topic
  3. Print the swapped array.
int main()
{
	char arr[] = "abcdefg";
	reverse_string(arr);
	printf("%s\n",arr);
	return 0;
}

Customize the strlen() function to calculate the length of the array

int my_strlen(char* arr)
{
	int count = 0;
	while (*arr!='\0')
	{
		count++;
		arr++;
	}
	return count;
}

Method One (non-recursive)

  1. Define the function type: here is just to exchange arrays, no parameters are required to return, so void is used.
  2. Define two pointers: char* left and char* right, which point to the address of the first element of the array and the address of the last element of the array respectively.
  3. Swap array elements.
void reverse_string(char* arr)
{
	char* left = arr;
	char* right = arr + my_strlen(arr) - 1;
  
//my_strlen计算出的数组长度包括了‘\0’,因此-1获得整个数组的长度,再+arr指向最后一个元素的地址。
	
    while (left < right)
	{
		char tmp = *left;
		*left = *right;
		*right = tmp;
		left++;
		right--;
	}
}

Method 2 (recursive)

Problem-solving ideas:

  1. In the array char arr[] = "abcdefg \0" , the first element and the last element are exchanged each time, plus the exchange function of the middle element.
  2. Such as: first swap "g reverse_string(char* arr) a \0" . Note: Don't rush to put a into the address of the last element, let the last array element be '\0', and store a in the temporary variable tmp. For example: "g reverse_string(char* arr) \0", tmp=a. (Reason: The inverse function is passed in a string. If you do not do this, the inverse function cannot be called again, because the remaining elements in the middle are not a string at this time)
  3. There must be a restriction to achieve recursion. If there is no restriction, dead recursion will occur. When the length of the string is an odd number, there is only one array element left in the middle, and there is no need to exchange. Add the restriction condition if(strlen(arr)>1) to stop the recursion at the end.
  4. Finally put the first element into the address of the last element.
void reverse_string(char* arr)
{
	char* left = arr;
	char* right = arr + my_strlen(arr) - 1;
	char tmp = *left;  //临时变量,暂时存放第一个数组元素
	*left = *right;
	*right = '\0';
	if(strlen(arr)>1)
	reverse_string(arr + 1);  //left指针指向第二个数组元素
	*right = tmp;
}

The overall implementation and results of the code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int my_strlen(char* arr)
{
	int count = 0;
	while (*arr!='\0')
	{
		count++;
		arr++;
	}
	return count;
}

非递归版本
//void reverse_string(char* arr)
//{
//	char* left = arr;
//	char* right = arr + my_strlen(arr) - 1;
//	while (left < right)
//	{
//		char tmp = *left;
//		*left = *right;
//		*right = tmp;
//		left++;
//		right--;
//	}
//}

void reverse_string(char* arr)
{
	char* left = arr;
	char* right = arr + my_strlen(arr) - 1;
	char tmp = *left;
	*left = *right;
	*right = '\0';
	if(strlen(arr)>1)
	reverse_string(arr + 1);
	*right = tmp;
}

int main()
{
	char arr[] = "abcdefg";
	reverse_string(arr);
	printf("%s\n",arr);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_59174190/article/details/121401533