Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize string rotation in C language

Shulou Source: shulou.com Published: 2022-06-03 15:04:18 09月23日 Update

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

Tags: Characters strings are made up of languages functions source code first words learning variables left-handed methods same position elements ideas more results rules procedures help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Linux macOS MySQL vpn