1. //非指针,非递归
//#include<stdio.h>
//
//int Strlen(char* str)
//{
// if (*str != '\0')
// {
// return 1 + Strlen(str + 1);
// }
// return 0;
//}
//
//void reverse(char* str)
//{
// int l = 0;
// int r = Strlen(str) - 1;
// while(l<r)
// {
// int temp = 0;
// temp = str[l];
// str[l] = str[r];
// str[r] = temp;
// l++;
// r--;
//
// }
//}
//
//int main()
//{
// char arr[7] = { "abcdef" };
// reverse(arr);
// printf("%s", arr);
// return 0;
//}
//
//
2. //指针,非递归
//#include<stdio.h>
//#include<string.h>
//
//
//void reserve(char* str)
//{
// int l = 0;
// int r = strlen(str) - 1;
// while (l < r)
// {
// char temp = 0;
// temp = *(str + l);
// *(str + l) = *(str + r);
// *(str + r) = temp;
// l++;
// r--;
// }
//}
//
//int main()
//{
// char arr[7] = { "abcdef" };
// reserve(arr);
// printf("%s\n", arr);
// return 0;
//}
3. //指针,递归
#include<stdio.h>
#include<string.h>
void reserve(char* str)
{
int r = strlen(str) - 1;
char temp = *str;
*str = *(str + r);
*(str + r) = '\0';//////把它的字符拿走应当放一个'\0'表示该字符串长度减少,而不是直接拿走
if (r >= 2)
{
reserve(str + 1);
}
*(str + r) = temp;
}
int main()
{
char arr[7] = { "abcdef" };
reserve(arr);
printf("%s\n", arr);
return 0;
}
C语言:倒序数组
于 2024-11-28 22:23:23 首次发布