Three ways to implement left-spin string in C language
Title:
Implement a function that can left-spin k characters in a string.
For example: ABCD left one character to obtain BCDA ABCD left two characters to obtain CDAB
Method 1:
Let's draw a picture and analyze it:
Basic logic:
That is, before each rotation, we take the first element of the arr array and store it in tmp, then we move every remaining element of the arr array one bit forward, and finally we put the element in tmp back to the end of the arr array.
Implementation algorithm:
We find that when abcd rotates k = 4 characters by 4 characters, the array returns to its own array abcd. Now, we can think of it this way, even though we rotated 4 characters, we actually rotated 0 characters; And so on, by rotating 5 characters, you actually rotate 1 character; And so it goes on...... Next, we lengthen the character length to abcde 5 characters, rotate k = 5 characters, the array will return to its own array abcde, and the following analysis is the same as the 4 character analysis. Then, we can get the expression num(actual number of counts) = k % len(number of array characters).
Code implementation:
#include <stdio.h> #include <string.h> void Turn_left(char arr[],int k) { int len = strlen(arr); // Find the true number of rotations int num = k % len; for (int i = 0; i < num; i++) {// Move one char tmp = arr[0]; int j = 0; for (j = 0; j < len - 1; j++) {// Mobile data arr[j] = arr[j + 1]; } arr[j] = tmp; } } int main() { char arr[] = "abcd"; int k = 0; scanf("%d", &k); // Enter the number of rotating characters Turn_left(arr, k); printf("%s\n", arr); return 0; }
Running result:
Method 2:
We can also draw a picture to analyze:
Here we are going to use two functions:
strcpy() // String copy strcpy(str1,str2)// Copies the elements of str2 into str2 strncat()// This is also string copy strncat(tmp,arr,k)// Copies the elements from arr to the end of the string tmp, Copy k
Code implementation:
#include <stdio.h> #include <string.h> void Turn_left(char arr[], int k) { int len = strlen(arr); int num = k % len; char tmp[1000] = { 0 }; // Copy the string from arr + num to strcpy(tmp, arr + num); // Copy the element at the beginning of arr to the end of the string tmp, copying num strncat(tmp, arr, num); // Finally copy the entire tmp array element into the arr array strcpy(arr, tmp); } int main() { char arr[] = "abcd"; int k = 0; scanf("%d", &k); // Enter the number of rotating characters Turn_left(arr, k); //2 printf("%s\n", arr); return 0; }
Running result:
Method 3:
Finally, let's draw a picture and analyze it:
Code implementation:
#include <stdio.h> void Reverse(char arr[], int i, int j) { while (i < j) { char tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } } void Turn_left(char arr[], int k) { int len = strlen(arr); int num = k % len; // Reverse the element before the num position (arr, 0, num-1); // Reverse the num position and the elements following it (arr, num, len-1); // The entire array is in Reverse order (arr, 0, len-1); } int main() { char arr[] = "abcd"; int k = 0; scanf("%d", &k); // Enter the number of rotating characters Turn_left(arr, k); //2 printf("%s\n", arr); return 0; }
Running result:
The above is the details of the three ways to implement left-handed strings in C language, more information about left-handed strings in C language please pay attention to other related articles in Script home!
Related article
-
Issues related to the member initialization list of C++ classes
The following Xiaobian will bring you an article about the member initialization list of C++ classes. Xiaobian think quite2016-05-05 -
Parse the constructors and some special forms of multilevel derivation in C++
This article mainly introduces the parsing of C++ multi-layer derivation of the constructor and some special forms, special forms mainly for the base class and subobject type of the constructor content, the need for friends can refer to the next2015-09-09 -
How does C++ tell if four points form a square
This article mainly introduces the C++ how to determine whether four points form a square case, has a good reference value, I hope to help you. Let's take a look2021-03-03 -
Compiler implementation of lambda expressions in C++
C++ 11 adds a very important feature - Lambda expressions. This article mainly introduces the compiler implementation principle of lambda expression in C++, and the friends who need it can refer to it2017-02-02 -
Detail C++ virtual function table storage location
I believe we know the location of the virtual table pointer and virtual function storage, but the storage location of the virtual function table can not be determined for a while. This article will talk about the relevant content in detail with you, I hope to help you2023-04-04 -
When a project becomes large, we will introduce many libraries, so that two libraries are likely to define the List, Tree, Node class with the same name, if the compiler does not take this into account, the programmer will have a conflict problem when calling. C++ provides namespace tools to better control the scope of the name, this article to talk about C++ namespaces, need friends can refer to it2021-09-09
-
Implicit function declarations in C
In the c language, it is still necessary to learn C ++ programming habits, and you must declare before using functions. Otherwise, even if the compilation passes, there may be some inexplicable problems at runtime.2016-01-01 -
The basic knowledge of source code, inverse code and complement code is introduced in detail
This article explains the computer source code, inverse code and complement. And conducted an in-depth exploration of why to use the inverse code and complement, and further demonstrated why you can use the inverse code, the addition of the complement to calculate the subtraction of the source code, the need of friends can refer to the next2016-12-12 -
An in-depth look at rvalue references in C++
Here to take you in-depth interpretation of rvalue reference in C++, rvalue reference is an important feature in the new C++ standard, including C++11 reference folding, first of all, let's take a look at the concept of rvalue reference:2016-05-05
Latest comments