(1423)编写函数实现字符串循环右移n位
来源:互联网 发布:童年 知乎 编辑:程序博客网 时间:2024/06/04 19:33
#include <cstdlib>#include <iostream>#include <cstring>#define MAXLEN 20/*编写循环函数,使一个字符串循环右移n个字符*/using namespace std;void LoopMove1(char *str,int n){ char *p=str; char temp[MAXLEN]; int len=strlen(str); int remain=len-n;//记录剩余的的字符串长度 strcpy(temp,str+remain); strcpy(temp+n,str); *(temp+len)='\0'; strcpy(p,temp); }void LoopMove2(char *pstr,int n){ int remain=strlen(pstr)-n; char temp[MAXLEN]; memcpy(temp,pstr+remain,n); memcpy(pstr+n,pstr,remain); memcpy(pstr,temp,n);}int main(int argc, char *argv[]){ char pstr[]="abcdefghi"; int n=2; LoopMove1(pstr,n); cout<<pstr<<endl; char pstr2[]="abcdefghi"; LoopMove2(pstr2,n); cout<<pstr2<<endl; system("PAUSE"); return EXIT_SUCCESS;}