#include <stdio.h>
#include <string.h>
#define MAX_LEN 16
void rotate_right(char *pstr, int steps);
int main(void)
{
char arr[] = "abcdefghi";
int step;
printf("Before rotate right move string is:%s\n", arr);
printf("Enter your step:");
scanf("%d", &step);
rotate_right(arr, step);
printf("After rotate right move string is:%s\n", arr);
return 0;
}
/*
void rotate_right(char *pstr, int steps)
{
int n = strlen(pstr) - steps;
char temp[MAX_LEN];
strcpy(temp, pstr + n);
strcpy(temp + steps, pstr);
*( temp + strlen(pstr)) = '\0';
strcpy(pstr, temp);
}
*/
void rotate_right(char *pstr, int steps)
{
int n = strlen(pstr) - steps;
char temp[MAX_LEN];
memcpy(temp, pstr + n, steps);
memcpy(pstr + steps, pstr, n);
memcpy(pstr, temp, steps);
}
转载请注明原文地址: https://ju.6miu.com/read-1297911.html