#include <iostream>
#include <string>
using namespace std;
void LoopMoveRight(char *vStr, int steps);
void LoopMoveRightByStrCopy(char *vStr, int steps);
void LoopMoveRightByMemcpy(char *vStr, int steps);
int main()
{
char g_str[20] = "\0";
int steps;
cout<<"Please input the string: ";
cin>>g_str;
cout<<"Please input the steps moving rightly: ";
cin>>steps;
LoopMoveRight(g_str,steps);
cout<<"After moving, the string is : "<<g_str<<endl;
cout<<"Please input the steps moving rightly: ";
cin>>steps;
LoopMoveRightByStrCopy(g_str, steps);
cout<<"After moving, the string is : "<<g_str<<endl;
cout<<"Please input the steps moving rightly: ";
cin>>steps;
LoopMoveRightByMemcpy(g_str, steps);
cout<<"After moving, the string is : "<<g_str<<endl;
return 0;
}
void LoopMoveRight(char *vStr, int steps)
{
int len = strlen(vStr);
char tempChar;
while(steps > 0)
{
int i = len - 1;
tempChar = vStr[i];
for (; i > 0; i--)
{
vStr[i] = vStr[i - 1];
}
vStr[0] = tempChar;
steps--;
}
}
void LoopMoveRightByStrCopy(char *vStr, int steps)
{
int RemanLen = strlen(vStr) - steps;
char tmpStr[10];
strcpy(tmpStr, vStr + RemanLen);//get the sub string which will be put in the front
strcpy(tmpStr + steps, vStr);
*(tmpStr + strlen(vStr)) = '\0';//same as tmpStr[strlen(vStr)]
strcpy(vStr, tmpStr);
}
void LoopMoveRightByMemcpy(char *vStr, int steps)
{
int RemanLen = strlen(vStr) - steps;
char tmpStr[10];
memcpy(tmpStr, vStr + RemanLen, steps);
memcpy(vStr + steps, vStr, RemanLen);
memcpy(vStr, tmpStr, steps);
}