字符串移位:如“abcdefghi”右移2位后变成“cdefghiab”
函数头:
//pStr 是指向以'\0'结尾的字符串指针 //steps 是要求移动的步数 void LoopMove(char *pStr, int steps) { //...... }
使用库函数实现:
方法一:
#define MAX_LEN 1000 void LoopMove(char *pStr, int steps) { int n=strlen(pStr)-steps; char tmp[MAX_LEN]; strcpy(tmp,pStr+n); strcpy(tmp+seps,pStr); *(tmp+strlen(pStr))='\0'; strcpy(pStr,tmp); }
方法二:
#define MAX_LEN 1000 void LoopMove(char *pStr, int steps) { int n=strlen(pStr)-steps; char tmp[MAX_LEN]; memcpy(tmp, pStr+n, steps); memcpy(tmp+steps, pStr, n); memcpy(pStr, tmp, n+steps) }