How to realize string rotation in C language
This article mainly introduces "how C language realizes string rotation". In daily operation, I believe many people have doubts about how C language realizes string rotation. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how C language realizes string rotation". Next, please follow the editor to study!
There are two ways to think about this topic:
1. The postposition of the first character and the forward push of the rest of the elements repeat this process K times (k is the number of left-handed characters).
Eg:
The string is "ABCDEF\ 0" and needs to be rotated by 2 characters
First save the first character to a variable, and then place all the remaining variables one bit earlier, that is, "BCDEF\ 0" (note here that the last Terminator\ 0 does not advance, leave a space).
Then give the variable used to save to the last position of the string (except\ 0), that is, "BCDEFA\ 0"
To do this process again is "CDEFGA\ 0"-"CDEFGAB\ 0"
Source code:
# include#include#includevoid Exchange (char * str, int k) {int iMagne; char temp; char * sta; for (I = 0; I < k; iSum +) {sta = str; temp = * str;// saves the first character for (m = 0; m