Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

GreyMatta

macrumors regular
Original poster
Jul 29, 2007
212
0
England
is this a valid if statement


if ((CharA) != (CharB || (CharB - 32) || (CharB + 32)))
{
palindrome = false
}


If CharA is not equal to either (CharB), (CharB - 32) or (CharB + 32) I need a true output to get the palindrome = false

for some reason it wont work :confused:

i have been on this for about 6 hours now going round in circles

any pointers would be very helpfull
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Try:

Code:
if ( (CharA != CharB) && (CharA != (CharB - 32)) && (CharA != (CharB + 32)) )
{
    palindrome = false;
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
It looks like you want to do case-insensitivity. Kainjow's answer was correct, but ASCII is a funny thing, so I wouldn't trust + AND - 32 (! would equal A), just use toupper, like so:
Code:
#include <iostream>
using std::cout;
using std::endl;

#include <cctype>
using std::toupper;

int main(int argc,char *argv[]) {
 char CharA;
 char CharB;

 CharA='u';
 CharB='U';
 if(toupper(CharA) == toupper(CharB)) {
   cout << CharA << " matches " << CharB << " insensitive to case." << endl;
 }
 CharA='t';
 CharB='U';
 if(toupper(CharA) == toupper(CharB)) {
   cout << CharA << " matches " << CharB << " insensitive to case." << endl;
 }
}
 

GreyMatta

macrumors regular
Original poster
Jul 29, 2007
212
0
England
The problem is I have to use -32 to identify the uppercase or lowercase characters in a palindrome (its for school)

kainjows answer I think is on the right track but doesn't seem to work quite right

here is my full code

Code:
int main(int argc, char* argv[])
{
AnsiString Line;
int Front;
int Back;
bool Palindrome;
char CharA;
char CharB;
        Line = ReadStringPr("Enter the string to be tested. ");
        Front = 1;
        Back = Length(Line);
        Palindrome = true;
          while ((Front < Back) && (Palindrome == true))
              {
              CharA = Line[Front];
              CharB = Line[Back];
                if ((CharA != CharB) && (CharA != (CharB - 32)) && (CharA != (CharB + 32)))
                  {
                  Palindrome = false;
                  }
                else
                  {
                  Front = Front + 1;
                  Back = Back - 1;
                  }
               }
           WriteString(Line);
           if (Palindrome == true)
                {
                WriteStringCr(" is a palindrome");
                }
           else
                {
                WriteStringCr(" is not a palindrome");
                }
        getchar();
        return 0;
}

It has to check so that it ignores uppercase and lowercase

for eg these would all be palindromes

AbBa
aBbA
ABbA
abba
ABBA etc

I am pulling my hair out

lee1210 I am sure your answer is correct but it to advanced for what I have been taught already so I cant really use it.

I know in my head my answer should work but it just doesnt
 

alaceo

macrumors member
Feb 21, 2008
32
0
GreyMatta,

Is it specified that you must use +-32? It seems odd because that can be unsafe on different systems...I'm not even sure it works both ways like that, my ASCII is verrrry rusty. toupper(), as lee1210 stated, is an easy and safe way. The function allows the value to be interpreted as uppercase, so 'A' will equal 'a' because they are both temporarily read as 'A'.

Quite simple:
Code:
if ((CharA != CharB) && (CharA != (CharB - 32)) && (CharA != (CharB + 32)))

Would be reduced to:

Code:
if ( toupper(CharA) != toupper(CharB) )

And it would still do what you'd like.
 

jcgnu

macrumors regular
Dec 30, 2006
113
0
I would do what's suggested above... Use the toupper function so they all become ABBA. That way, you don't care about any of the other stuff...

Plus, sometimes programming logic does not follow human logic... Then, if you wanted to say "if 'a' is different than either 'b' or 'c'" it won't work this way:

if (a != (b || c))... which would be the human way.

It has to be...

if ((a!=b) && (a!=c))


But again, in your case, it's not necessary. Just use the toupper (or toUpperCase, or whatever, I don't remember) and problem solved.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
is this a valid if statement


if ((CharA) != (CharB || (CharB - 32) || (CharB + 32)))
{
palindrome = false
}

It is a valid statement except for the missing semicolon after "false" that will set palindrome to false if and only if CharA == 1. Please check your books to find what the || operator does, and don't proceed until you can show that the expression (CharB || (CharB - 32) || (CharB + 32)) always produces a result of 1.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
The problem is I have to use -32 to identify the uppercase or lowercase characters in a palindrome (its for school)

kainjows answer I think is on the right track but doesn't seem to work quite right

here is my full code

<snip>

It has to check so that it ignores uppercase and lowercase

for eg these would all be palindromes

AbBa
aBbA
ABbA
abba
ABBA etc

I am pulling my hair out

lee1210 I am sure your answer is correct but it to advanced for what I have been taught already so I cant really use it.

I know in my head my answer should work but it just doesnt

I would still insist, if you are not allowed to use things from the libraries, on making your own toupper in your program and using that. It would be extremely easy:

Code:
char myToUpper(char input) {
  char result;
  if(input >= 'a' && input <= 'z') { //Lower case letter
    result = input + ('A'-'a'); //It's fine to do 32, but unless you know ascii by heart, this is clearer.
  } else {
    result = input;
  }
  return result;
}

I am not too familiar with the AnsiString class, I'm not too experienced with C++, but does Length(AnsiString) return the same thing as AnsiString.Length()?

If what you have isn't working, I would dig in with a debugger (if you are using gcc/g++ to compile, add the -g option, then invoke gdb on the program produced), or fall back to the good old debug print statements. I would print what is returned from the Length call, then what Front and Back are each iteration of the loop, and what CharA and CharB are. If you want to really check things out, you can also print the values of:
(CharA != CharB)
(CharA != (CharB - 32))
(CharA != (CharB + 32))

or if you make your own toupper, just:
(myToUpper(CharA) != myToUpper(CharB))

I didn't see anything glaringly wrong. I thought I saw an off-by-one with the indicies for AnsiString, but didn't realize it was 1-based (i think it might damage my psyche to work with a C-based language using classes that were 1-based instead of 0-based).

Good luck.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.