Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods of realizing K characters in a function left-handed string in C language?

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

This article mainly explains "what are the methods of C language to realize the K characters in a function left-handed string". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "what are the methods of C language to realize the K characters in a function left-handed string?"

Method 1. Perform this action once to the left for k times

Let me briefly introduce the idea of this method:

Existing 1 string abcd

We move an out, and then there will be a place in front of the bcd.

Move the bcd forward one position, and finally put an into the last position to complete a left-handed rotation.

So it's done once. What about left-handed k times? In fact, it is to perform the above operation once more.

The code is as follows (example):

# include#includevoid left_move (char*arr, int k) / / arr is the address of the first element. You can use a pointer to receive {assert (arr); int I = 0; int len = strlen (arr); / / calculate the length for of the arr (I = 0 Bing I < k Ting iaddress +) {int j = 0 Char tmp = * arr; for (j = 0 tmp; j < len-1 tmp; +) {arr [j] = arr [j + 1]; / / arr [j] = * (arr+j)} arr [len-1] = tmp;} int main () {int x = 0 Char arr [] = "abcd"; printf ("enter how many characters you want to turn left:\ n"); scanf ("% d", & x); left_move (arr, x); printf ("% s\ n", arr);}

Ps: an extra assert is used here to prevent the null pointer passed by the function from being asserted. If the null pointer is passed, the system will report an error.

Method 2. String in reverse order multiple times

Let's take the left rotation of ABCDEF twice as an example:

Left-handed twice, that is, move the AB behind the CDEF. Let's see what I do. First, AB and CDEF are divided into two groups.

Reverse the two groups separately

Finally, the final result CDEFAB is obtained in reverse order.

So left-handed k times, that is, you can change 2 into k, and the whole is still these three steps, which is very convenient.

The code is as follows (example):

# include#includevoid reverse (char*l, char*r) / / reverse function {assert (lumped roomr); while (l < r) {char tmp = * l; * l = * r; * r = tmp; lumbago; rmi- } void left_move (char*arr, int k) {assert (arr); int len = strlen (arr); reverse (arr,arr+k-1); / / first group reverse reverse (arr+k,arr+len-1); / / second group reverse reverse (arr,arr+len-1); / / whole reverse int main () {int x = 0; char arr [] = "abcdef" Printf ("enter how many characters you want to turn left:\ n"); scanf ("% d", & x); left_move (arr, x); printf ("% s\ n", arr);}

Ps: about the reverse function of each time: take abcd as an example, that is, an and d transposition, b and c transposition, get dcba, and then the reverse order is carried out three times to get the so-called left-handed.

Thank you for your reading, the above is the content of "what is the method of realizing K characters in a function left-handed string in C language?" after the study of this article, I believe you have a deeper understanding of the method of realizing K characters in a function left-handed string in C language, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Left-handed character reverse function string method language that is position pointer whole learning two code content that is ideas examples movement input Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple Shulou Information Redmi OPPO Reno