例:char ch[]="international";
i n t e r n a t i o n a l 变为 r n a t i o n a l i n t e
(1)将ch[0]个到第ch[4]字符与最后交换
l a n o
r n a t i
e t n i
(2)反转ch[9]到ch[12]的部分
l a n o r n a t i
i n t e
(3)反转ch[0]到ch[8]的部分
i t a n r o n a l
i n t e
(4)反转ch[0]到ch[4]的部分
r n a t i
o n a l i n t e
#include <stdio.h>
#include <string.h>
void fun (char * ptr,int m)
{
int lenth=strlen(ptr)-1;
int i;
int j;
//将第1个到第m字符与最后交换
for(i=0,j=lenth;i<m;i++)
{
char tmp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=tmp;
j--;
}
//反转lenth-m+1到lenth的部分
for(i=lenth-m+1,j=lenth;i < j;i++,j--)
{
char tmp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=tmp;
}
//反转0到lenth-m的部分
for(i=0,j=lenth-m; i < j;i++,j--)
{
char tmp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=tmp;
}
//反转0到lenth-m-m的部分
for(i=0,j=lenth-m-m;i < j;i++,j--)
{
char tmp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=tmp;
}
}
int main(int argc, char const *argv[])
{
char ch[50]={0};
int lenth;
int m;
printf("please input character\n");
scanf("%s",ch);
printf("please input m\n");
scanf("%d",&m);
fun(ch,m);
printf("%s\n",ch);
return 0;
}