PDA

View Full Version : C++ problem please help




GreyMatta
Mar 26, 2008, 08:39 PM
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
Mar 26, 2008, 08:54 PM
Try:

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

lee1210
Mar 26, 2008, 09:00 PM
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:
#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
Mar 27, 2008, 02:06 AM
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

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
Mar 27, 2008, 02:23 AM
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:

if ((CharA != CharB) && (CharA != (CharB - 32)) && (CharA != (CharB + 32)))


Would be reduced to:


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


And it would still do what you'd like.

jcgnu
Mar 27, 2008, 02:27 AM
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
Mar 27, 2008, 06:23 AM
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
Mar 27, 2008, 09:33 AM
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:

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

GreyMatta
Mar 27, 2008, 10:44 AM
I have just tried it using toupper.

Thanks for the sugestions eveyone :D