The C language is used to implement the left right and right handed string problems
- 2020-06-07 05:03:11
- OfStack
1. Implement 1 function that can left-rotate k characters in the string.
ABCD Left-rotate 1 character to get BCDA
ABCD Left-rotate two characters to get CDAB
1. Violent displacement method
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void left_move(char*str, int k)
{
int len = 0;
int i = 0;
while (k--)
{
//1. The first 1 I'm gonna save it
char tmp = *str;
//2. I'm going to move backwards
len = strlen(str);
for (i = 0; i < len; i++)
{
*(str + i) = *(str + 1 + i);
}
//3. Put the saved data last 1 position
*(str + len - 1) = tmp;
}
}
int main()
{
char arr[] = "ABCD";
int n = 0;
printf(" Please enter the sinistral number: ");
scanf_s("%d", &n);
left_move(arr, n);
printf("%s ", arr);
system("pause");
return 0;
}
2.3 Flip method
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
void reverse(char *left, char* right)
{
assert(left&&right);// assertions
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void left_move(char*str, int k)
{
int len = strlen(str);
reverse(str, str + k - 1);// Reverse the first half
reverse(str+k, str + len - 1);// The second half of the inversion
reverse(str, str+ len - 1);// Reverse the entire string
}
int main()
{
char arr[] = "ABCD";
int n = 0;
printf(" Please enter the sinistral number: ");
scanf_s("%d", &n);
left_move(arr, n);
printf("%s ", arr);
system("pause");
return 0;
}
2. Implement 1 function that can right-rotate k characters in the string.
ABCD Right-rotate 1 character to get DABC
ABCD Right-handed two characters give CDAB
1. Violent displacement method
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void right_move(char* p, int k)
{
int len = strlen(p);
for (int i = 0; i < k; ++i)// Control rotation number
{
char tmp = p[len - 1];
for (int j = len - 1; j > 0; --j)// complete 1 A rotating
{
p[j] = p[j - 1];
}
p[0] = tmp;
}
}
int main()
{
char arr[] = "ABCD";
int n = 0;
printf(" Please enter the dextral digit: ");
scanf_s("%d", &n);
right_move(arr, n);
printf("%s ", arr);
system("pause");
return 0;
}
2.3 Step inversion method: the whole string is first reversed, followed by the first half of the inversion and the second half of the inversion or
First the second half of the inversion, then the first half of the inversion, and finally the whole inversion can be
The specific code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
void reverse(char *left, char* right)
{
assert(left&&right);// assertions
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void right_move(char*str, int k)
{
int len = strlen(str);
reverse(str, str + len-k -1);// Reverse the first half
reverse(str+len-k, str + len - 1);// The second half of the inversion
reverse( str ,str + len - 1);// Reverse the entire string
reverse(str, str + len - 1);// Reverse the entire string
reverse(str, str + k - 1);// Reverse the first half
reverse(str+k, str + len - 1);// The second half of the inversion
}
int main()
{
char arr[] = "ABCDEFG";
int n = 0;
printf(" Please enter the dextral digit: ");
scanf_s("%d", &n);
right_move(arr, n);
printf("%s ", arr);
system("pause");
return 0;
}
conclusion