使用递归函数时,程序不会打印字符串的第一个字符。 [英] The program is not printing the first character of the string while using recursion function.

查看:121
本文介绍了使用递归函数时,程序不会打印字符串的第一个字符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//使用递归来反转字符串

#include< stdio.h>

#include< string.h>

虚无趣(char * str);

void main()

{

char str [30];

static int i;

printf(输入一个字符串:);

得到(str);

fun(& str [0] );

}

虚无趣(char * str)

{

int len;

len = strlen(str);

if(len)

{

fun(++ str);

}

printf(\ n%c,* str);



}



根据输出,printf()在++ str之后打印值,因此省略字符串的第一个字符。 Ex-帮帮我是字符串然后输出是:em。



我尝试过:



但是,如果我按照这个简单的程序,那么输出是0123,它不会省略第一个整数。

void main()

{

abc(3);

}

void abc(int a)

{

如果(a)

{

abc(a-1);

}

printf (%d,a);

}



建议请。在此先感谢。

//Reverse a string using recursion
#include<stdio.h>
#include<string.h>
void fun(char *str);
void main()
{
char str[30];
static int i;
printf("Enter a string:");
gets(str);
fun(&str[0]);
}
void fun(char *str)
{
int len;
len=strlen(str);
if(len)
{
fun(++str);
}
printf("\n %c",*str);

}

As per the output the printf() is printing the values after ++str, hence omitting the first character of a string. Ex- "Help me" is string then output is: em ple.

What I have tried:

But if i follow this simple program, then the output is 0123 and it not omitting the first integer.
void main()
{
abc(3);
}
void abc(int a)
{
if(a)
{
abc(a-1);
}
printf("%d",a);
}

Suggestions please. Thanks in advance.

推荐答案

++ str就是其中的原因。首先放置++意味着它是预先递增的。指针先递增,然后传递给fun。您可能会发现后期增量是您想要的。这将是str ++。这会在它的值被推到堆栈后增加str。



就个人而言,我不喜欢这些结构,因为它们很容易被忽视。我会把它写成两个独立的语句 - 首先调用函数然后递增指针。
The "++str" is the cause of that. Placing the ++ first means it is pre-incrementing. The pointer is incremented first and then it is passed to fun. You might find that post-incrementing is what you want here. That would be "str++". This would increment str after its value is pushed on the stack.

Personally, I prefer neither of those constructs because they can be easily overlooked. I would write that as two independent statements - first call the function and then increment the pointer.


引用:

程序在使用递归函数时不会打印字符串的第一个字符。

The program is not printing the first character of the string while using recursion function.



您的代码行为不符合您的预期,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现bug,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试你的程序(踩踏和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


正如 Rick 指出 ++ str 是错误的。诀窍是将(str + 1)传递给递归调用,尝试:



As pointed out by Rick ++str is the mistake. The trick is passing (str+1) to the recursive call, try:

#include<stdio.h>

#define MAXLEN 30

void print_reversed(const char *str);

int main()
{
  char str[MAXLEN];
  printf("Enter a string:\n");
  fgets(str, MAXLEN, stdin);
  print_reversed(str);
  printf("\n");
  return 0;
}

void print_reversed(const char *str)
{
  if ( *str=='\n' || *str=='\0') return;
  print_reversed( str + 1);
  printf("%c",*str);
}


这篇关于使用递归函数时,程序不会打印字符串的第一个字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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