<> subject 1

String left rotation
Implement a function , Can be left-handed in the string k Characters .
for example ：
ABCD One character left to get BCDA
ABCD Two characters left to get CDAB

Thinking analysis
Create a temporary variable to hold the first character of the string , utilize for Loop through string , To assign the last bit to the previous bit , Finally, assign the first character to the last element .

<> code implementation
// Implement a function , Can be left-handed in the string k Characters . void leftturn(char* str,int length,int k) { // Loop implementation Character left rotation
for (int i = 0; i < k; i++) { int n = 0; // Use a temporary variable to store the first character char tmp = str;
// Traversing arrays , To assign the last bit to the previous bit for (int n = 0; n < length - 1; n++) { str[n] = str[n + 1];
} // Implement the assignment of the first character to the last element str[length-1] = tmp; } } int main() { char str[] = "abcd";
int length= strlen(str); int k = 0; printf(" Please enter the number of left-handed characters ："); scanf("%d", &k);
printf("before：%s\n", str); leftturn(str,length,k); printf("after：%s\n", str);
system("pause"); return 0; }
Code process analysis

<> subject 2

String rotation result
Write a function , To determine whether a string is a rotated string of another string .
for example ：
given s1 =AABCD and s2 = BCDAA, return 1
given s1=abcd and s2=ACBD, return 0.

Thinking analysis

This problem is based on the previous one , The judgment of string rotation result is added , You can create a decision function Judge, Loop calls left and right rotation functions , To determine whether the string of each loop result is and str equal , If it's what we're looking for find, Then return 1 that will do ~

<> code implementation
// Levorotation k Character function void leftTurn(char* str,int length,int k) { for (int i = 0; i < k; i
++) { int n = 0; char tmp = str; for (int n = 0; n < length - 1; n++) { str[n
] = str[n + 1]; } str[length-1] = tmp; } } // Dextral rotation k Character function void rightTurn(char* str,
int length, int k) { for (int i = 0; i < k; i++) { int n = 0; char tmp = str;
for (int n = 0; n < length - 1; n++) { str[n] = str[n + 1]; } str[length - 1] =
tmp; } } // compare Whether the two strings meet the requirements int judge(char* str, char* find) { int length1 = strlen
(str); int length2 = strlen(find); // The two strings are different in length , Direct return 0 if (length1 != length2) {
return 0; } // Use cycle call Left handed and right handed functions , Get the result of each change for (int i = 0; i < length1; i++) {
leftTurn(str, length1, i); if (strcmp(str, find) == 0) { return 1; } rightTurn(
str, length1, i++); if (strcmp(str, find) == 0) { return 1; } } return 0; } int
main() { char s1[] = "AABDC"; char s2[] = "BCDAA"; printf("%s\n%s\n", s1, s2);
int ret=judge(s1, s2); printf("%d\n", ret); system("pause"); return 0; }
<> Supplementary notes

call strcmp function , Header file is required <string.h>
strcmp The prototype of the function is ：int strcmp（const char * str1,const char * str2）
strcmp Is to compare whether two strings are equal , This function begins to compare the first character of each string . If they are equal to each other , Then continue to compare , Until the characters are different or the terminated null character is reached

Return value representation
<0 The first mismatched character is in the str1 The value in is higher than that in str2 The value in is low
0 The contents of two strings are equal
>0 The first mismatched character is in the str1 The value in is greater than that in str2 Values in

Technology
Daily Recommendation