Thread: Palindrome Problems

  1. #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");
    	
    
    	}
    	
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if(palindrome = 0) /*always false!*/
    		printf("This Is Not A Palindrome");
    
    else if(palindrome = 1) /*always true!*/
    		printf("This Is A Palindrome");
    Last edited by anon; 11-20-2009 at 05:28 PM.
    I might be wrong.

    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).

  3. #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.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    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).

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  6. #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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks, Albinosword. I can barely read/not read, that highlighted text, and missed that.

  8. #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?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  10. #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");
    	
    
    	}
    	
    }

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.

  13. #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.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.

  15. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread