Palindrome in strings

This is a discussion on Palindrome in strings within the C Programming forums, part of the General Programming Boards category; We have to create a simple program to detect palindromes, mine basically does that, but now we have to modify ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Palindrome in strings

    We have to create a simple program to detect palindromes, mine basically does that, but now we have to modify it so it ignores case and ignores punctuation.

    I modified it to ignore the case by just simply turning all letters in the string to uppercase, but i'm stuck on implementing the isalpha() function to ignore punctuation marks, any help appreciated. Cheers

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int isPalindrome(char s[]);
    int main (void){
    
      char s[100];
    
      printf("Enter string:\n"); //MAIN FUNCTION GETS THE INPUT
      fgets(s, 100, stdin);
    
      isPalindrome(s);
    
    return 0;
      }
    
    int isPalindrome(char s[]){   //THIS FUNCTION DOES THE STRING CHECKING
    
      int len;
      int lo, hi;
    
      for(len = 0; s[len] != '\0'; len++);    //GETTING LENGTH OF STRING
    
    
        lo = 0;                                  //LO is the starting of the array
        hi = len - 2;                          //HI is the length of the array - 2.
        
        while(hi > lo){
          
          s[hi] = toupper(s[hi]);                     //CONVERSION TO UPPERCASE
          s[lo] = toupper(s[lo]);
          
          if(s[lo] != s[hi]){                                              //THE CASE FOR IF IT'S NOT A PALINDROME
            printf("Not a palindrome\n"); 
            printf("&#37;c %d %c %d\n", s[lo], lo, s[hi], hi);       //DEBUGGING CHECKS
            return 0;
          }
          hi--;
          lo++;
          printf("%c %d %c %d\n", s[lo], lo, s[hi], hi);        
        }
    
          printf("Yes a palindrome\n");
        return 0;
      }
    Last edited by JFonseka; 08-31-2007 at 05:57 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ignoring punctuation (and spaces?) would be a case of remvoing them from the input string - best way would probably be to use a loop to copy the string, skipping any unwanted characters such as "'.,!\"&%$() " and such. strchr can help you here. - or use "isalpha()", and if you want to support numerical palindromes combine in "isdigit" with an OR.

    --
    Mats

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by matsp View Post
    Ignoring punctuation (and spaces?) would be a case of remvoing them from the input string - best way would probably be to use a loop to copy the string, skipping any unwanted characters such as "'.,!\"&%$() " and such. strchr can help you here. - or use "isalpha()", and if you want to support numerical palindromes combine in "isdigit" with an OR.

    --
    Mats
    So I essentially copy the string into a new string but having ignored all the punctuations and spaces then continue with the normal checking, is this what you mean?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Either copy to a new string, without all the punctuation, or take it into account when working out the next pair of characters to compare.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JFonseka View Post
    So I essentially copy the string into a new string but having ignored all the punctuations and spaces then continue with the normal checking, is this what you mean?
    That would be my "simple" solution, yes. It's not MUCH harder to just skip over them, but at least if you do it the way you have, where you start at either end and search to the middle, you may introduce bugs when trying to deal with "skippable" characters on either side...

    Also consider that the input may end up being "nothing" if you remove spaces and punctuation characters.

    By the way, you may as well translate the characters to upper (or lower) case too, when you are converting them.

    --
    Mats
    Last edited by matsp; 08-31-2007 at 06:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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