The code's function is decribed as follow,but it doesn't work.The problem must be caused by the line "strcpy(pStr,temp);//This line caused the problem,so why?"
--but i don't know why.Any suggestion will be helpful,thanks advanced.
Code:
//This function loop shift the string right by nsteps,for example,if "1234" input,the output will be "3421"
#include <string.h>
#include <iostream.h>
#define MAX_LEN 20
int LoopMove(char *pStr,int nSteps);
int main()
{
char *pStr = "0123";
int nSteps = 2;
LoopMove(pStr,nSteps);
return 0;
}
int LoopMove(char *pStr,int nSteps)
{
char temp[MAX_LEN];
int n;
n = strlen(pStr) - nSteps;
strcpy(temp,pStr + n);
strcpy(temp + nSteps,pStr);
*( temp + strlen(pStr) )= '\0';
strcpy(pStr,temp);//This line caused the problem,so why?
cout<<pStr<<endl;
cout<<temp<<endl;
cout<<strlen(pStr)<<" equal to "<<strlen(temp)<<endl;
return 0;
}