c语言递归实现 字符串/数组翻转
程序员文章站
2022-05-23 09:19:16
...
c语言递归实现 字符串/数组翻转
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
/*****tonytrek*****2021.5.7*****/
/*****数组/字符串翻转*****/
void string_upside_down(int,int,char *,int);
int main()
{
char* string=malloc(10);
scanf("%s",string);
int num1=0;
int num2=strlen(string)-1;
int rest=strlen(string);
string_upside_down(num1,num2,string,rest);
printf("%s\n",string);
return 0;
}
void string_upside_down(int num1,int num2,char *str,int rest)
{
char tmp;
if(rest==1||rest==0)
{
return;
}
else
{
tmp =str[num1];
str[num1]=str[num2];
str[num2]=tmp;
rest=rest-2;
num1++;
num2--;
string_upside_down(num1,num2,str,rest);
}
}