Any suggestion?

This is a discussion on Any suggestion? within the C++ Programming forums, part of the General Programming Boards category; I am guessing something is not right. When I input a palindrome string for example 'madam' is says Not palindrome. ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Question Any suggestion?

    I am guessing something is not right. When I input a palindrome string for example 'madam' is says Not palindrome. Your help is appreciated.


    #include<iostream.h>
    #include<string.h>
    //#include<stdlib.h>

    int main()
    {
    //clrscr();
    char arr[20]; //array to hold string
    int len,ans=0,x,y;

    cout<<"Enter : ";
    cin>>arr; // string input

    len=strlen(arr); // finding out string length,using a lib func

    for (x=0,y=(len-1); x!=y ; x++,y--) //multiple var for loop
    {
    if (arr[x] == arr[y]) //increment palindrome checking counter ans++;
    if (ans == (len/2) )// if palindrome checking counter is equal to half the string length,output"palindrome"
    {
    cout<<"Its a palindrome !!";
    }
    }

    cout<<"Not a palindrome !";


    return 0;
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You don't have anything increment when the letters match. Make sure you have ans++ after your if statement.

    Also, change your for statement to say x<y instead of x!=y. Otherwise, even number word lengths will send it into an infinite loop since for example length 2:

    x=0 y=1
    x=1 y=0
    x=2 y=-1
    etc.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Hasn't changed

    I did changed to your suggestion but it didn't work for some reason. Thanks though.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    1
    The previous post was right on. You need to make sure the condition for the for..loop is x<y and you need to increment ans++. The one thing that wasn't mentioned in the post is that you need to initialize ans to 1 not 0. This will solve the problem. BTW cout<<"Not a palindrome !"; will always print because there is no condition statement testing whether it should or not. You'll see this after you run your program with the corrections.

    Good Luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LCD for gaming, any suggestion?
    By alphaoide in forum Tech Board
    Replies: 0
    Last Post: 05-02-2004, 05:09 PM
  2. Replies: 12
    Last Post: 05-14-2003, 01:00 AM
  3. 3 x 3 square contest suggestion
    By badman in forum Contests Board
    Replies: 0
    Last Post: 02-10-2003, 11:12 PM
  4. any suggestion - pointer, strtok, function etc
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-13-2002, 01:04 AM
  5. Any suggestion?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 04:24 AM

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