#include<stdio.h>
#include<string.h>
void connect(char *st1, char *st2, char *q)
{
for(;*st1!='\0';)
{
*q=*st1;
st1++;
q++;
}
for(;*st2!='\0';)
{
*q=*st2;
st2++;
q++;
}
*q='\0';
}
int main()
{
char soul[20];
printf("请输入字符串: \n");
gets(soul);
int len,m;
printf("请输入想要交换的位置m,(注意m须小于总字符串长度): \n");
scanf("%d",&m);
len = strlen(soul);
char *goal1 = new char[m+1];
char *goal2 = new char[len-m+1];
memcpy(goal2,&soul[m],len-m);
memcpy(goal1,&soul[0],m);
char c[30];
char *p;
p=c;
connect(goal2,goal1, p);
printf("%s\n",c);
delete []goal1;
delete []goal2;
delete []p;
return 0;
}
c语言输入一个字符串,输入整数m,从m处交换前后两段的位置,使用指针
最新推荐文章于 2024-07-25 21:23:23 发布