![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 6
| Palindrome Problems 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 | |
| | #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:
Last edited by anon; 11-20-2009 at 05:28 PM. | |
| anon is offline | |
| | #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 | |
| | #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)
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #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 | |
| | #6 | |
| Registered User Join Date: Sep 2008
Posts: 27
| Quote:
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 | |
| | #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 | |
| | #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 | |
| | #9 | |
| Registered User Join Date: Sep 2006
Posts: 3,142
| Quote:
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 | |
| | #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 | |
| | #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 | |
| | #12 |
| Registered User Join Date: Jan 2009
Posts: 221
| Also you should use double equal if you are doing comparisons, or it's an assignment. |
| Subsonics is offline | |
| | #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 | |
| | #14 | |
| Registered User Join Date: Jan 2009
Posts: 221
| Quote:
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 | |
| | #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 | |
![]() |
| Tags |
| c program, if statement, palindrome program |
| Thread Tools | |
| Display Modes | |
|
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 |