?第一种方法非递归
#include<stdio.h>
int main()
{
char arr[] = "abcdef";
int left = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
int right = sz - 2;
while (left < right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
printf("%s", arr);
}
?第二种方法非递归
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = "abcdef";
int left = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
int right = strlen(arr)-1;
while (left < right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
printf("%s", arr);
}
第三种方法,递归?
#include<stdio.h>
#include<string.h>
void reverse(char arr[])
{
int left = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
int right = strlen(arr) - 1;
while (left < right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
printf("%s", arr);
}
int main()
{
char arr[] = "abcdef";
reverse(arr);
}
用三种方法都能实现逆序,合理选择方法学习。