#include<stdio.h>
void main()
{
char str[] = {'a','b','c','d','e'};
char temp = {0};
int left = 0;
int right = sizeof(str)-1;
int count = 0;
while (left < right)
{
count++;
temp = str[right];
str[right] = str[left];
str[left] = temp;
left++;
right--;
}
printf("%s\r\n",str);
printf("runtime = %d\r\n",count); //查看一共做了几次运算
}
[C] 字符串数组翻转参考示例
最新推荐文章于 2025-11-06 13:43:54 发布
此博客展示了一段C语言代码,通过定义字符数组,使用双指针法实现数组元素反转。代码中设置左右指针,在左指针小于右指针时交换元素,同时记录交换次数,最后输出反转后的数组和交换次数。
800

被折叠的 条评论
为什么被折叠?



