#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int inverse(char* str);
void main()
{
char buf[]="abcdefgh";//初始化字符串
inverse(buf);//调用接口函数
printf("反转之后的字符串为:%s\n",buf);//打印字符
system("pause");
}
int inverse(char* str)//创建接口函数
{
char* tempstr=str;
char* p1;//字符串开头初始化
char* p2;//字符串结尾初始化
char temp;//交换的中间变量
if(str==NULL)
{
return -1;
}
p1=tempstr;//tempstr所指的地址,实际指向的是buf字符数组的开头地址
p2=tempstr+strlen(tempstr)-1;//tempstr所指的地址,实际指向的是buf字符数组的结尾地址(通过画内存四区图即可弄懂)
while(p1<p2)//通过指针修改buf中的值
{
temp = *p1;
*p1=*p2;
*p2=temp;
++p1;
--p2;
}//运行完while函数之后,buf数组中存的值已经改变
return 0;
}
字符串反转方法2(调用接口函数)
最新推荐文章于 2022-06-05 11:59:58 发布