查找字符串回文的代码 [英] Code for finding string palindrome

查看:118
本文介绍了查找字符串回文的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定代码执行没有错误但有一些逻辑问题..请找到它们



我尝试过:



the given code executes without bug but has some logical problems.. kindly find them

What I have tried:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
	clrscr();
	int i,len,j=0,flag,k;
	char str[100],king[100];
	cout<<"enter a string";
	gets(str);
	cout<<" the string you have entered is "<<str<<endl;
	len=strlen(str);
	for(i=0;i<len;++i)>
	{
		if(str[i]!=' ')
		{
			king[j]=str[i];
			j++;
		}
		if(str[i]==' ')
		{

			for(int f=0, k=1;king[f]{
				if(king[f]==king[j-k])
				{
					flag=1;
				}
				else
				{
					flag=0;
					break;
				}
				j=0;
			}
		}

	}
	if(flag==1)
	cout<<"YOU HAVE ENTERED A STRING PALINDROME which is"<<king;
	if(flag==0)
	cout<<"you have not entered any string palindrome ";
	getch();
}

推荐答案

给定代码执行时没有错误但有一些逻辑问题

不,不是。它编译时没有编译器错误,但它有bug。这就是逻辑错误的含义。



请找到他们

No.



我们不会为你做功课!



所以,这取决于你。

在函数的第一行放置一个断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不会为你做那些 - 时间让你学习一门新的(非常非常有用的)技能:调试!
"the given code executes without bug but has some logical problems"
No it doesn't. It compiles without compiler errors, but it has bugs. That is what "logical errors" means.

"kindly find them"
No.

We don't do your homework for you!

So, it's going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we don't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


你应该学会尽快使用调试器尽可能。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

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

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


作为一名近40年来一直在编写代码的专家,让我告诉你,因为你的代码编译并不意味着它没有bug和工作。



成为专家需要什么?学习使用调试器。



一旦开始使用调试器,你就会意识到他们不在那里调试代码。他们在那里调试你。它们有助于纠正您对代码实际操作的理解,而不是仅仅猜测它并祈祷它有效。



您将从调试代码中学到更多知识永远学会要求别人为您修复代码。这就是专家的作用。
As an "expert" who's been writing code for nearly 40 years now, let me tell you, just because your code compiles doesn't mean that it is bug free and works.

What does it take to become an "expert"? Learning to use debuggers.

Once you start using the debuggers you'll realize that they are not there to debug the code. They are there to debug YOU. They help correct your understanding of what your code really does instead of you just guessing at it and praying it works.

You will learn more from debugging the code than you will ever learn from asking someone else to fix your code for you. THAT'S what an "expert" does.


这篇关于查找字符串回文的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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