题目要求不能开额外的数组,空间不允许
要求完成以下结果,比如输入abcdefgh 给定n为3则使得字符数组顺序变为defghabc
递归实现如下:
#include<stdio.h>
#include<string.h>
void swap(char *s1 ,int ls1, char *s2,int ls2){
if(ls1 == 0 || ls2 == 0) return;
if(ls1 == ls2){
for(int i = 0 ; i < ls1 ; i++ ){
s1[i] = s1[i] + s2[i];
s2[i] = s1[i] - s2[i];
s1[i] = s1[i] - s2[i];
}
}
else if(ls1 < ls2){
swap(s1,ls1,s2,ls1);
swap(s2,ls1,s2+ls1,ls2-ls1);
}
else {
swap(s1+ls1-ls2,ls2,s2,ls2);
swap(s1,ls1-ls2,s1+ls1-ls2,ls2);
}
}
int main(){
char s[100];
scanf("%s",s);
int n ;
scanf("%d",&n);
int len = strlen(s);
swap(&s[0],n,&s[n],len-n);
printf("%s\n",s);
return 0;
}