Thread: new a problem

  1. #1
    Registered User
    Join Date
    Mar 2005

    new a problem

    Hey... I've seen this question asked about time and time again. I've done searches and such, but just can't find the answer to my question. I've been working on code for a program that determents is a phrase is a palindrome or not. Now I'm not that far in c++ so most of these other posts involve stuff we have not yet talked about. I am trying to make this code using loops. I'm using while statements, if statements, else statements. I am using getline to get the statement from the user and and i use substrings to separate the letters. I just cannot get my if else statements right to make this work. Any help would be appreciated.


  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    I think I can explain it so you can come up with a good solution without providing you with any actual code. A string is a char array that has a determinable length. What you want to do is use two indices. Before you begin looping, one of them ("i") refers to the first letter of the string and the other ("j") to the last. Then each time through the loop you check for two things. 1) If index i is has passed index j (meaning it is greater or equal) the string is a palindrome because you ran out of characters to compare. 2) If the character at index i is not identical to that at index j, it is not a palindrome. Then you increment the i index and decrement the j index to check the next character. You see, you're starting on the ends of the string and moving inward.

    Hope that helps. Share your progress and further problems.
    Last edited by LuckY; 03-23-2005 at 09:20 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    I just cannot get my if else statements right to make this work. Any help would be appreciated.
    Are you doing something like this:

    char str1[20];
    char str2[20];
    if(str1 == str2)
    If you are, that doesn't work because an array name, e.g. str1 or str2, is really a unique id, which is assigned to the array name when the array is declared. You will learn about those unique id's when you study pointers. For now, just know that this is what you are really doing in your if statement:
    if(143658 == 567942)
    Since unique numbers can never be equal, the code in the if block will be skipped.

    To compare char arrays, there is a function called strncmp(). It takes two char arrays as parameters, e.g.

    strncmp(str1, str2)

    and returns 0 if they are equal, so the equivalent of:

    if(str1 == str2)


    if(strncmp(str1, str2) == 0)

    Have you studied the string type yet? If you have they are much easier to use than char arrays. You can access characters in a string type just like you do in an array, so for your program, you could use a for-loop starting at str1.length() - 1, and ending when 0 is reached. Inside the loop, you can add the character at postion i in str1:


    to str2. When the loop ends, you will have the reversed string stored in str2. Then, with string types you can do a normal if statement comparison:

    if(str1 == str2) //then it's a palindrome
    Last edited by 7stud; 03-24-2005 at 12:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM