Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the given code executes without bug but has some logical problems.. kindly find them

What I have tried:

C++
#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();
}
Posted
Updated 9-Jul-16 4:00am
v3

"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!
 
Share this answer
 
Comments
[no name] 9-Jul-16 2:25am    
hey just say why <"you have not entered any string palindrome> output comes even when i enter a string palindrome.. i did the "homework" and now u should correct it as experts//
OriginalGriff 9-Jul-16 2:38am    
:laugh:
You've done the *first part* of the homework: you've written the code you think does the job.
Now you've tested it and it fails - so the second part of the job it to find out why and fix the problem.
It's like trying to do anything: make a paper plane for example. Folding paper so it looks sleek and has wings is easy, but getting it to fly in a straight line means changing it, tweaking it, throwing-it-away-and-starting-again!
Developing code is the same: it isn't finished until it works.
Think about your other homework: writing an essay isn't over when you hammer out a rough draft - you have to polish it, tweak it, add bits, take bits away before you end up with something you can submit. If you don't, you get a poor grade.
So give it a try - debugging is the fun bit!
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[^]
 
Share this answer
 
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.
 
Share this answer
 
Comments
Patrice T 9-Jul-16 11:45am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900