#include <iostream>
#include <assert.h>
using namespace std;
//编写一个函数,作用是把一个char组成的字符串循环右移n个
//调用strcpy函数方式实现
void LoopMove1(char *pStr,int steps){
assert(pStr!=NULL);
char tmp[20];
int n=strlen(pStr)-steps;
strcpy(tmp,pStr+n);
strcpy(tmp+steps,pStr);
*(tmp+strlen(pStr))='\0';
strcpy(pStr,tmp);
}
//调用memcpy函数方式实现
void LoopMove2(char *pStr,int steps){
assert(pStr!=NULL);
char tmp[20];
int n=strlen(pStr)-steps;
memcpy(tmp,pStr+n,steps);
memcpy(tmp+steps,pStr,n);
memcpy(pStr,tmp,strlen(pStr));
}
void main(){
char a[]="abcdfemuc";
LoopMove1(a,2);
LoopMove2(a,3);
cout<<a<<endl;
system("pause");
}
剖析:
这个试题主要考查面试者对标准库函数的熟练程度,在需要的时候引用库函数可以很大
程度上简化程序编写的工作量。
最频繁被使用的库函数包括:
(1) strcpy
(2) memcpy
(3) memset