Exercise 3.31 days

Write a function reverse_string (char * string) (recursive)
implementation: the parameter string of characters oppositely oriented.
Requirements: You can not use string manipulation functions in the C library.

#include <string.h>

int my_strlen(char* str)
{
	int count = 0;
	while (*str != '\0')
	{
		count++;
		str++;
	}
	return count;
}
void reverse_string(char* str)
{
	char tmp = str[0];//1
	int len = my_strlen(str);
	str[0] = str[len - 1];//2
	str[len - 1] = '\0';//3
	if (my_strlen(str+1) >=2)
		reverse_string(str+1);//4
	str[len - 1] = tmp;//5
}

int main()
{
	char arr[] = "hello bit";
	//"tib olleh"
	reverse_string(arr);
	printf("%s\n", arr);

	return 0;
}

Released nine original articles · won praise 0 · Views 166

Guess you like

Origin blog.csdn.net/doudou0309/article/details/105234337