C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-20-2009, 05:08 PM   #1
Registered User
 
Join Date: Nov 2009
Posts: 6
Palindrome Problems

Hello,
Im hoping someone can help me. Im trying to write a program to determine if a word or phrase is palindrome or not, the same letters forward as backwards (bob, level, madam im adam). No matter what my keyboard input is, it always sets my "palindrome" flag to "1". I even added lines to verify my array was being read correctly. Even if the characters displayed are not the same, my if statement goes as true and sets the flag high. I have modified and tested it many times, and Im sure the problem is the If statement that compares the individual characters. Im sure it is something I have messed up, but I cant seem to find what it is. The code is pasted below. Any help would be much appreciated.

Code:
// project created on 11/18/2009 at 4:20 PM
#include <stdio.h>
#include <string.h>

 main()
{
	{
//The Following Lines Are Declarations And Initializations //
	int x, first, last, palindrome = 1;
	char str[100];

//The Following Lines Are User Input Statements//		
	printf("Enter A Word Or Phrase To Check Whether It Is A Palindrome Or Not-              ");
	printf("No Spaces Or Punctuation Please.  	  					 ");
	scanf("%s",str);
	
//The Following Lines Are Declarations and Initializations//	
	first = 0;
	x = strlen(str);
	last = x - 1;
	
//The Following Two Lines Were Added To Assist Me In Debugging//
//This Proves The Values Were Read Correctly From The Array//	
printf ("The Beginning First Character Is %c.							 ",str[first]);	
printf ("The Beginning Last Character Is %c.							 ",str[last]);

//The Following While Loop Sets The Loop For The String Length//
while (first <= last)
	{
		
//The Following If/Else If Statement Evaluates Individual Characters In The str String//		
if	(str[last] == str[first])
		first = (first + 1),last = (last - 1);	
else 
		palindrome = 0, last = 0;	
}


//The Following If/Else Statement Takes The Result From The Character Evaluation And Displays The Result//
if(palindrome = 0)
		printf("This Is Not A Palindrome");
	
else if(palindrome = 1)
		printf("This Is A Palindrome");
	

	}
	
}
obeygiant is offline   Reply With Quote
Old 11-20-2009, 05:25 PM   #2
The larch
 
Join Date: May 2006
Posts: 3,222
Code:
if(palindrome = 0) /*always false!*/
		printf("This Is Not A Palindrome");

else if(palindrome = 1) /*always true!*/
		printf("This Is A Palindrome");
__________________
I might be wrong.

Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).

Last edited by anon; 11-20-2009 at 05:28 PM.
anon is offline   Reply With Quote
Old 11-20-2009, 05:37 PM   #3
Registered User
 
Join Date: Nov 2009
Posts: 6
I tried switching my flag statement there, but the error always followed the "if (str[last] == str[first])" statement.I appreciate it though.
obeygiant is offline   Reply With Quote
Old 11-20-2009, 06:12 PM   #4
The larch
 
Join Date: May 2006
Posts: 3,222
I was trying to hint that you are evaluating assignments there.

Code:
if (x = 0)
assigns 0 to x and then tests if x is non-zero (it never is).
__________________
I might be wrong.

Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 11-20-2009, 09:45 PM   #5
Registered User
 
Join Date: Sep 2006
Posts: 3,142
When you highlight code (a great idea), use only the darker colors. I can barely read your highlighted text, above.

Last edited by Adak; 11-20-2009 at 11:41 PM.
Adak is offline   Reply With Quote
Old 11-20-2009, 10:48 PM   #6
Registered User
 
Join Date: Sep 2008
Posts: 27
Quote:
Originally Posted by Adak View Post
In C you can't evaluate strings directly against one another.

if(string1 == string2)

can never be right - you are checking pointer addresses when you do that.

Use strcmp(string1, string2); instead.

Code:
if((strcmp(string1, string2)) == 0)
  //the strings match. otherwise the result will be < 0  or > 0 (typically -1 and 1).
P.S. When you highlight code (a great idea), use only the darker colors. I can barely read your highlighted text, above.

But the OP is using character by character comparisons, so using if(string[0] == string[4]) is perfectly legit, the [] is already dereferencing the pointer.
Albinoswordfish is offline   Reply With Quote
Old 11-20-2009, 11:16 PM   #7
Registered User
 
Join Date: Sep 2006
Posts: 3,142
Thanks, Albinosword. I can barely read/not read, that highlighted text, and missed that.
Adak is offline   Reply With Quote
Old 11-20-2009, 11:53 PM   #8
Registered User
 
Join Date: Nov 2009
Posts: 6
Sorry about the highlighting error. Thanks everyone for your help. So, if I have dereferenced the pointer and the characters are being evaluated against each other, why am I still always reading the if statement as true, no matter what the input string being evaluated?
obeygiant is offline   Reply With Quote
Old 11-21-2009, 03:00 AM   #9
Registered User
 
Join Date: Sep 2006
Posts: 3,142
Quote:
Originally Posted by obeygiant View Post
Sorry about the highlighting error. Thanks everyone for your help. So, if I have dereferenced the pointer and the characters are being evaluated against each other, why am I still always reading the if statement as true, no matter what the input string being evaluated?
Did you fix the == error yet?

For comparisons in C, you need TWO equal signs, not just one: if(variable1 == variable2), not if(variable1 = variable2). The second version is just an assignment, not a comparison, as was stated above by Anon.

Post up your current code. I thought your problems were toast by now.
Adak is offline   Reply With Quote
Old 11-21-2009, 07:43 AM   #10
Registered User
 
Join Date: Nov 2009
Posts: 6
Here is the current code. Right now it feels like Im toast, not the problem.

Code:
// project created on 11/18/2009 at 4:20 PM
#include <stdio.h>
#include <string.h>

 main()
{
	{
//The Following Lines Are Declarations And Initializations //
	int x, first, last, palindrome;
	char str[100];

//The Following Lines Are User Input Statements//		
	printf("Enter A Word Or Phrase To Check Whether It Is A Palindrome Or Not-              ");
	printf("No Spaces Or Punctuation Please.  	  					 ");
	scanf("%s",str);
	
//The Following Lines Are Declarations and Initializations//	
	first = 0;
	x = strlen(str);
	last = x - 1;
	
//The Following Two Lines Were Added To Assist Me In Debugging//
//This Proves The Values Were Read Correctly From The Array//	
printf ("The Beginning First Character Is %c.							 ",str[first]);	
printf ("The Beginning Last Character Is %c.							 ",str[last]);

//The Following While Loop Sets The Loop For The String Length//
while (first <= last)
	{
		
//The Following If/Else If Statement Evaluates Individual Characters In The str String//		
if (str[first] == str[last])
		first = (first + 1),last = (last - 1);	
else 
		palindrome = 0, last = 0;	
}


//The Following If/Else Statement Takes The Result From The Character Evaluation And Displays The Result//
if(palindrome = 0)
		printf("This Is Not A Palindrome");
	
else if(palindrome = 1)
		printf("This Is A Palindrome");
	

	}
	
}
obeygiant is offline   Reply With Quote
Old 11-21-2009, 08:03 AM   #11
Registered User
 
Join Date: Jan 2009
Posts: 221
As far as I can see you never set palindrome to 1 if it's true. You also have an extra pair of braces in there that might disturb things (I'm not sure about that but), and unrelated to the problem, main should return int.

Last edited by Subsonics; 11-21-2009 at 08:06 AM.
Subsonics is offline   Reply With Quote
Old 11-21-2009, 08:10 AM   #12
Registered User
 
Join Date: Jan 2009
Posts: 221
Quote:
Originally Posted by obeygiant View Post
Code:
//The Following If/Else Statement Takes The Result From The Character Evaluation And Displays The Result//
if(palindrome == 0)
		printf("This Is Not A Palindrome");
	
else if(palindrome == 1)
		printf("This Is A Palindrome");
	
	
}
Also you should use double equal if you are doing comparisons, or it's an assignment.
Subsonics is offline   Reply With Quote
Old 11-21-2009, 09:39 AM   #13
Registered User
 
Join Date: Nov 2009
Posts: 6
OK Ive updated that. Thanks. Now I have palindrome = 0 in my initialization statement. It should only be set to 1 at the str[first] == str[last] statement, if true. No matter what, this statement always goes true.
obeygiant is offline   Reply With Quote
Old 11-21-2009, 10:14 AM   #14
Registered User
 
Join Date: Jan 2009
Posts: 221
Quote:
Originally Posted by obeygiant View Post
OK Ive updated that. Thanks. Now I have palindrome = 0 in my initialization statement. It should only be set to 1 at the str[first] == str[last] statement, if true. No matter what, this statement always goes true.
If you declare it with the value of 0, and you changed to == where you check if palindrome is 1 or 0 it can never be 1. You never set palindrome to 1 in your code, if you set it to 1 in your declaration it will work.

That means that palindrome = 1, unless str[first] == str[last] fails.

Last edited by Subsonics; 11-21-2009 at 10:16 AM.
Subsonics is offline   Reply With Quote
Old 11-21-2009, 10:49 AM   #15
Registered User
 
Join Date: Nov 2009
Posts: 6
Hahahaha. Ok guys I got it. The problem was fixed earlier I just didnt know it. An ankle-biter bug. Im using MonoDevelop In Ubuntu and my kid accidentally messed up my settings. All my rebuilds were'nt being compiled to the current program. Grrrr. Thank all of you very much for your assistance. Definitely learned alot today. Kudos guys.
obeygiant is offline   Reply With Quote
Reply

Tags
c program, if statement, palindrome program

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in Recursive String Palindrome Code clegs C Programming 13 12-21-2008 12:36 PM
No clue how to make a code to solve problems! ctnzn C Programming 8 10-16-2008 02:59 AM
Is it a Palindrome? xp5 C Programming 3 09-06-2007 05:26 AM
Palindrome Coding trouble TheLoneWolf32 C++ Programming 3 02-22-2003 07:05 PM
palindrome HELP !!! TED22_8 C Programming 23 01-22-2002 02:14 PM


All times are GMT -6. The time now is 10:56 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22