#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    7
    Rep Power
    0

    C Program for palindromes


    Howdy,

    We have been told for school to write a program to test if a string entered is a palindrome. Its supposed to return 1 if it is a palindrome and 0 if its not. It also needs to ignore punctuation, spaces , capitals etc....
    e.g this should world

    Madam, I'm Adam.

    this is my code, however it keeps printing that everything (even the above, and "kayak" is NOT a palindrome). any suggestions.

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    
    int isPalindrome( char *s );
    
    int main ( void )
    {
       int i = 0;
       int ch;
       char s[100];
       
       while ((ch = getchar()) != '\n') {
          if (isalpha(ch)) {
             s[i] = ch;
             i++;
          }    
       }
       
       if ( isPalindrome(s) == 1) {
          printf("Yes, is a palindrome.\n");
       } else { 
          printf("No, not a palindrome.\n");
       }
          
       return 0;
       
    }
       
    int isPalindrome( char *s )
    {
       int i = strlen(s)-1;
       int j = 0;
       
       while (j<=i) {
          if(s[j] != s[i]) {
             return 0;
          }
          i--;
          j++;
       }
       return 1;
    }
    Just thought i would add that we need to use a function in a main like above, and that we haven't learnt the whole C language. also variables (including *s) are as per the requirements.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    planet<earth_like_t>
    Posts
    365
    Rep Power
    149
    Will you also be taught how to use a debugger? Cuz, programming without one is like painting while blind-folded.

    Your code is perfect except for one minor detail. I ran your code and in the function isPalindrome(), the variable i was incorrect to start with. That's because the string that's passed doesn't have a terminator character so the function strlen() will not return the value you expect.
    Last edited by KitKat77; August 30th, 2006 at 04:15 PM. Reason: clarification
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    7
    Rep Power
    0
    Oh thats a releif,

    may i ask how i would correct that problem, i just tried adding like '\0' but that didnt work :(

    THanks tho!
  6. #4
  7. No Profile Picture
    Redpill
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2005
    Posts
    1,660
    Rep Power
    151
    And exactly how did you add the '\0'?


    You could change the "<=" test in the loop to "<". I'll leave it up to you to figure out why.

    It's also better practice to declare isPalindrome() as taking a const char* -- this tells the compiler that the string is not to be modified in the function, and allows you to pass string literals to isPalindrome().
    Last edited by jafet; August 30th, 2006 at 11:11 PM.
    Code:
    #include <stdio.h>
    int main(int o,char**O){return o>-1?o-2||!main(-1,1+O)?!!fprintf(stderr,"%s [0-"
    "9]{81}\n",*O):main(-83,++O):o>-83?(*O)[-1-o]?81==(o=-o-1)||o[*O]<'0'||'9'<o[*O]
    ?0:main(-2-o,O):o==-82:o>-164?(*O)[-83-o]<'1'?main(o-82,O):main(--o,O):o+164?o>-
    246?(*O)[-165-o]<'1'?main(o-82,O):main(--o,O):o+246?o>-328?(*O)[o=-o-247]<='8'?(
    main(-328-o,(++o[*O],O)),main(-247-o,O)):!(o[*O]='0'):(o=-o-328)<729?(o%9/3*3-o%
    27+o/243*9+o/81%3&&(*O)[o%81]==(*O)[o%81-o%27+o%9/3*3+o/243*9+o/81%3])||(o%81-o%
    9-o/81*9&&(*O)[o%81]==(*O)[o%9+o/81*9])||(o/81-o%9&&(*O)[o%81]==(*O)[o%81-o%9+o/
    81])?0:main(-409-o,O):main(-165-o%81,O):!puts(*O):0                           ;}
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    7
    Rep Power
    0
    Im sorry i still don't see any problem with my code... :(

    Sorry if i seem slow.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Well when you exit your loop reading characters, do you do
    s[i] = '\0';

    Post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Dinesh_P_V
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    India
    Posts
    259
    Rep Power
    0
    Either add
    s[i]='\0';
    after the end of while loop
    or
    declare s as
    char s[100]={'\0'};
    Dinesh_P_V
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    7
    Rep Power
    0
    Thankyou very much guys! I got it working by adding s[i] = '\0'.

    So you guys are saying that you need to add the end of string thing so it has the proper amount of array cells to count?

    I also added in ch = toupper(ch) because it wasn't working when I typed Kayak, for ex.

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    
    int isPalindrome( char *s );
    
    int main ( void )
    {
       int i = 0;
       int ch;
       char s[100];
       
       while ((ch = getchar()) != ('\n')) {
          if (isalpha(ch)) {
             ch = toupper(ch);
             s[i] = ch;
             i++;
          }
       }
       s[i]='\0';
       
       if ( isPalindrome(s) == 1) {
          printf("Yes, is a palindrome.\n");
       } else { 
          printf("No, not a palindrome.\n");
       }
          
       return 0;
       
    }
       
    int isPalindrome( char *s )
    {
       int i = strlen(s)-1;
       int j = 0;
       
       while (j < i) {
          if(s[j] != s[i]) {
             return 0;
          }
          i--;
          j++;
       }
       return 1;
    }
  16. #9
  17. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    > So you guys are saying that you need to add the end of string thing so it has the proper amount of array cells to count?
    All the str... functions which are not strn... functions need a \0 at the end of every string to mark the end of that string.

    All the strn... functions get an additional parameter indicating the maximum number of characters to process (unless a \0 is seen before that count is reached).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo