Thread: strings again

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    strings again

    you guys are gonna start hating me real quick but im sorry i have another question. I'm trying to write a program that will notice if a string is a palindrome or not (probably spelled that wrong). i.e anna, or racecar. thanks to anyone that helps and doesn't get too ........ed at me already.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    Well, my solution to this problem involved getting the length of the string. Scan every member of the array you store the string in up to the midpoint, pushing it into a stack.

    If the string is odd, skip the center letter (i.e., if the string is 5 letters long, we can skip the third letter), and pop an item off the stack, and compare it to the element in the array (in this instance the 4th letter).

    Basically the same thing for even sized strings, except you don't have to skip the middle letter.


    A stack is an ideal data structure for this because it basically stores the string in reverse. The trick is figuring out where to stop pushing, and where to start popping and comparing.

    starX
    www.axisoftime.com

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    well here is my code, but i can only get it to return 0 and not 1.
    int palindrome(char str[]);
    int main()
    {

    char str[20];

    int temp;
    int retval;
    printf("enter a string that is a palindrome:");
    fgets(str,sizeof(str),stdin);



    temp=palindrome(str);



    system("PAUSE");
    return 0;
    }
    int palindrome(char str[])
    {
    int i, j, retval;

    i = 0;
    j = strlen(str);
    retval = 1;
    while (i <= j)
    {
    --j;
    if (str[i] != str[j])
    {
    retval = 0;
    break;
    }
    i++;
    }
    printf("%d", retval);

    return retval; // return 1 if str is a palindrome, else return 0
    }

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    May I suggest a search here and at google as I have seen answers to the palindrome problem a few times before.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

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, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08: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. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM