在C递归反转一个字符串 [英] Reversing a string in c with recursion

查看:423
本文介绍了在C递归反转一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了code扭转在C字符串...它工作正常,但我不能回到在的main()函数的字符串逆转

I have written code to reverse a string in c... it works fine but I can't return the reversed string in the main() function.

#include<stdio.h>

main()
{
  char a[17]="abcdefg";
  reverse(a);
  printf("\n");
  system("PAUSE");
}
int reverse(char *a)
{
   if(*a!='\0')
   {   
     reverse(a+1);
   }
   printf("%c",*a);
}         

它打印逆转字符串,但我想在颠倒字符串的main()。我怎样才能做到这一点?

it prints the reversed string but I want the reversed string in main(). How can I do this?

推荐答案

您需要修改字符串,即输入缓冲器反向(),而不是仅仅印刷吧。

You need to modify the string, i.e. the input buffer to reverse(), instead of just printing it.

这样做递归似乎有点令人讨厌,但当然应该是可能的。

Doing this recursively seems a bit obnoxious, but should of course be possible.

基本上,我想打印变得转让,是这样的:

Basically, I guess the printing becomes an assignment, something like this:


  1. 基地:一个空字符串的逆转是空字符串

  2. 步骤:字符串的开始逆转通过交换的第一个和最后一个字符,然后递归在字符串的其余部分

这篇关于在C递归反转一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆