String reverse order - multiple methods

Reverse the order of strings, taking the string abcdef as an example

1. With additional arrays

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

int main() {
    char str[] = "abcdef";
    //Find the size of the string, including '\ 0'
    int sz = strlen(str) + 1;
    //Dynamic memory allocation, apply for a character array of sz size
    char* p = (char*)malloc(sizeof(char) * sz);
    int i = 0;
    int j = 0;
    for (i = 0, j = sz - 2; j >= 0; i++, j--) {
        p[i] = str[j];
    }
    //Add the character '\ 0' to the requested character array
    p[i] = '\0';
    //Copy back
    strcpy(str, p);

    printf("%s\n", str);
    return 0;
}

2. Cycle realization

2.1 diagram

2.2 ideas

A string can be understood as an array of characters with '\ 0' at the end. A single character of the string can be accessed through the array subscript.

  • The reverse order of the string does not include '\ 0' at the end
  • One method is to exchange the first character with the last character and the second character with the penultimate character except '\ 0' until all symmetrical characters are exchanged.
  • Define two variables: left record array as subscript, starting from 0; Right the right subscript of the record array, starting from the string length of - 1.
  • left+1, right-1 after each exchange.
  • Cycle exchange end condition: the cycle ends when left is greater than or equal to right.

2.3 code implementation

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

void reverse_str(char* str, int left, int right);

int main() {
	char str[] = "abcdef";
	int left = 0;
	int right = strlen(str) - 1;

	reverse_str(str, left, right);
	puts(str);

	return 0;
}
//
void reverse_str(char* str, int left, int right) {
	while (left < right) {
		char t = str[left];
		str[left] = str[right];
		str[right] = t;
		left++;
		right--;
	}
}

3. Recursive implementation 1

Idea: the idea is basically the same as that of loop implementation, except that the implementation method is realized by calling the function itself.

//Recursive implementation
#include <stdio.h>

void reverse_str(char* str, int left, int right);
int my_strlen(char* str);

int main() {
	char str[] = "abcdef";
	int left = 0;
	int right = my_strlen(str) - 1;

	reverse_str(str, left, right);

	printf("%s\n", str);
	return 0;
}
//Reverse order string
void reverse_str(char* str, int left, int right) {
	if (left < right) {
		char t = str[left];
		str[left] = str[right];
		str[right] = t;
		reverse_str(str, left + 1, right - 1);
	}
}
//Find string length
int my_strlen(char* str) {
	if (*str != '\0') {
		return 1 + my_strlen(str+1);
	}
	else {
		return 0;
	}
}

4. Recursive implementation 2

4.1 ideas

Recursive thinking - gradually reduce large problems to small ones.

  • Such as string abcdef
  • Reverse order string abcdef
  1. To reverse the order of the string abcdef, it is the reverse order of a and f plus the string bcde.
  2. If the string bcde appears, the f position must first be assigned '\ 0'.
  3. That is, when a and f are in reverse order, the character a cannot be stored immediately at position f. after the reverse order of the string bcde is completed, the character a can be stored at position f, so a needs to be stored with the temporary character variable t first; There is no limit on the position of character a, so the position of character a can directly store the value of character b.
  • Reverse order string bcde
  1. To reverse the order of the string bcde, it is the reverse order of b and c plus the string cd.
  • String cd in reverse order
  • Stop reverse order
  • Function to complete the reverse order

This kind of thinking is not very good. Come on!

#include <stdio.h>

void reverse_string(char* string);
int my_strlen(char* str);

int main() {
	char str[20] = "abcdef";
	reverse_string(str);
	puts(str);

	return 0;
}
//String reverse order
void reverse_string(char* string) {
	char t = *string;
	int len = my_strlen(string);
	*string = *(string + len - 1);
	*(string + len - 1) = '\0';
	if (my_strlen(string+1) >=2) {
		reverse_string(string + 1);
	}
	*(string + len - 1) = t;
}

//Find string length
int my_strlen(char* str) {
	if (*str != '\0') {
		return 1 + my_strlen(str + 1);
	}
	else {
		return 0;
	}
}

END

Tags: C Algorithm data structure

Posted by zechdc on Tue, 17 May 2022 03:07:02 +0300