I just cannot get my if else statements right to make this work. Any help would be appreciated.
Are you doing something like this:
Code:
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:
Code:
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)
is
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:
str[i]
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