In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.