Thread: Simple Palendrome Problem [newbie to C]

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    33

    Smile Simple Palendrome Problem [newbie to C]

    Hi All,

    Im trying to write a little programe that checks if a word entered is a palendrome or not.

    The programe runs but gives in-correct results, im realy stumped as to whats up, I wondered if any one could point me in the right direction.
    Many thanks
    Colin

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main()
    
    {
    char mystring[20],compstring[20];
    int length,loop,index=0;
    
    
    printf("Please enter a word under 20 letters to test : ");
    gets(mystring);
    
    length = strlen(mystring)-1;
    
       for(loop=length;length>0;length--)
       {
       compstring[index] = mystring[loop];
       index++;
       }
    
       if(!strcmp(mystring,compstring))
       {
       printf("The string Is A Palendrome");
       }
       else
       {
       printf("The string Is NOT A Palendrome");		      
       }
    
    }
    Last edited by colinuk; 11-17-2004 at 01:38 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're not nul-terminating compstring[] anywhere.

    Also:
    - Don't use void main
    - Don't use gets
    - Your spacing is atrocious
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Hi,
    Thanks for your reply, but to be honest in not sure i follow, how do I nul-terminating compstring[] ?

    I have edited the code and it just looks awful I agree, it looks great in my compiler but when I pasted it in ????

    Thanks again
    Colin

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    for(loop=length;length>0;length--)
    {
    	compstring[index] = mystring[loop];
    	index++;
    }
    Look carefully at that code you have. Which variable is the for loop decrementing each iteration? Is that variable used in the for loop block?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    A string is an array of characters followed by a null-terminating 0 (ASCII value 0). So in memory a string looks like r a c e c a r \0

    In your loop you're starting at strlen(mystring)-1 which is avoiding the \0 at the end of the loop and that's fine. But after the loop, compstring is left with everything except the \0. The only way functions like strcmp() know how big a string is is by running into that \0 as it's iterating through it. So after your loop you need to add that \0 to the end of compstring. That (along with decrementing the wrong variable in your for loop) should fix your problem.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Incorporating suggestions from everybody here:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        char mystring[20],compstring[20];
        int length,loop,index=0;
    
        printf("Please enter a word under 20 letters to test : ");
        fgets(mystring,sizeof(mystring),stdin);
    
        if( mystring[strlen(mystring)-1] == '\n' ) mystring[strlen(mystring)-1] = 0;
        length = strlen(mystring)-1;
    
        for(loop=length;loop>=0;loop--)
        {
        compstring[index] = mystring[loop];
        index++;
        }
        compstring[index] = 0; // Null terminate the string
    
        if(!strcmp(mystring,compstring))
        {
        printf("The string Is A Palendrome");
        }
        else
        {
        printf("The string Is NOT A Palendrome");		      
        }
    
    }
    [edit]Forgot some things, fgets will also extract and store the newline character so allowances must be made to take care of that and also the > in the for loop should be >= so that it copies over an extra character. See changes above in [/edit]
    Last edited by hk_mp5kpdw; 11-17-2004 at 02:20 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or it could be done without a second array:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char word[20];
      char *start, *end;
    
      printf("Please enter a word under 20 letters to test: ");
      fflush(stdout);
      fgets(word, sizeof(word), stdin);
    
      for(start = end = word;*end && *end != '\n';++end)
        ;
      *end-- = '\0';
    
      while(start < end)
      {
        if(*start != *end)
          break;
        start++;
        end--;
      }
    
      printf("%s is%s a palindrome.\n", word, start < end ? " not" : "");
    
      return 0;
    }
    Last edited by itsme86; 11-17-2004 at 02:03 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Hi guys,

    Thanks a lot for your comments,
    I reviewed my code and im still not sure how to actualy code the null termination mentioned??

    On the loop I made these changes after thinking about how it was working, but its still not quite right, sorry guys

    Code:
    for(loop=length;loop>0;loop--)
      {
      index=0;
      compstring[index] = mystring[loop];
      index++;
      }

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, you don't want to set index to 0 at the beginning of every loop iteration, just once before the loop begins. The thing that you're missing is after the loop completes, adding the null terminating zero after all the letters in the compstring array.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Forgot a couple of things in my post above, see 2 more changes made in red.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    This does not work either its like my code it constantly says Not a palendrome no matter whats entered??



    Quote Originally Posted by hk_mp5kpdw
    Incorporating suggestions from everybody here:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        char mystring[20],compstring[20];
        int length,loop,index=0;
    
        printf("Please enter a word under 20 letters to test : ");
        fgets(mystring,sizeof(mystring),stdin);
    
        if( mystring[strlen(mystring)-1] == '\n' ) mystring[strlen(mystring)-1] = 0;
        length = strlen(mystring)-1;
    
        for(loop=length;loop>=0;loop--)
        {
        compstring[index] = mystring[loop];
        index++;
        }
        compstring[index] = 0; // Null terminate the string
    
        if(!strcmp(mystring,compstring))
        {
        printf("The string Is A Palendrome");
        }
        else
        {
        printf("The string Is NOT A Palendrome");		      
        }
    
    }
    [edit]Forgot some things, fgets will also extract and store the newline character so allowances must be made to take care of that and also the > in the for loop should be >= so that it copies over an extra character. See changes above in [/edit]

  12. #12
    Yes,

    The first modification can be done simply with strrchr(), included in the string library. What this does is it finds the last occurrence of your character in the string. I started a thread about the easiest way to accomplish this task, though it came down to using strchr() or strrchr().

    Here is a sample of how strrchr() could be used:
    Code:
    char *pch;
    
    if (pch = strrchr(mystring, '\n'))
    	*pch = '\0';
    - Stack Overflow
    Last edited by Stack Overflow; 11-17-2004 at 02:31 PM. Reason: Fixed typo
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try adding this right before your strcmp function call to debug what is going on:

    Code:
    printf("mystring \"%s\" has length %d\n"
           "compstring \"%s\" has length %d\n",
           mystring,strlen(mystring),
           compstring,strlen(compstring));
    Both strings should be the same backwards and forwards and also have the same length.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    To debug try printf()'ing mystring and compstring. See if they're really coming up as you expect them to. If they both look right, then the problem is probably in the algorithm that determines if it's a palindrome. If they don't look right, then the problem is in the algorithm that creates compstring.

    Also, if the strings look different, seeing how they're different is usually a great indicator of what the problem is.
    If you understand what you're doing, you're not learning anything.

  15. #15
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Thank you all for your great responce of help.

    I finally got it with this code.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    
    {
    char mystring[20],compstring[20];
    int length,loop,index=0;
    
    
    printf("Please enter a word under 20 letters to test : ");
    gets(mystring);
    fflush(stdin);
    length = strlen(mystring)-1;
    
    for(loop=length;loop>=0;loop--)
    
       {
       compstring[index] = mystring[loop];
       index++;
       }
    compstring[index]=0;
    
       if(!strcmp(mystring,compstring))
       {
       printf("\nThe string Is A Palendrome");
       }
       else
       {
       printf("\nThe string Is NOT A Palendrome");
       }
    
    }
    Last edited by colinuk; 11-17-2004 at 07:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  3. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 06:41 PM
  4. Simple boolean problem
    By larry in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2001, 08:49 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM