I'm trying to use the tolower function from the cctype header ... in this function (to check if a word is a palindrome

int palindrome(const string& w)
{
	typedef string::size_type sz;
	string ret = w;
	sz r = 0;
	sz l = w.size() - 1 ;

	int check = 0;

	while(r != l && r < l)
	{
	 // needs to fix the upper / lower issue
		if(isupper(ret[r]))
			tolower(ret[r]);
		if(isupper(ret[l]))
			tolower(ret[l]);
		if(ret[r] == ret[l]){
			++r;
			--l;
		}
		if(ret[r] != ret[l]){
			r = l;
			check +=1;
		}
	}
	return check;
}

... so ... if the letter is uppercase ... it'll make it lowercase just to compare them ... but it just ... doesn't work ... the programs just finds AnA to be a palindrome, but Ana not ... can someone help me please?

Two things:

  1. tolower doesn't take a reference parameter, it returns the converted character if such a conversion exists. The immediate problem in your code is that you aren't saving the converted character back into ret[[I]x[/I]].
  2. tolower will accept any value in the range of unsigned char or EOF. If there's a lower case conversion, that value will be returned, otherwise the value you pass will be returned. As such, there's no reason to call isupper first. You can simply do this:
    ret[r] = tolower(ret[r]);
    ret[l] = tolower(ret[l]);

    It's generally best practice to cast the argument to unsigned char, but there are exceptions to that guideline, so I won't get into it now. It's not a big enough issue that you can't wait until a more appropriate time to learn about the various idiosyncrasies of char.

Ill suggest a simple solution to the problem

use stricmp() to compare the charachter.
So no problem of uppercase or lowercase.

>use stricmp() to compare the charachter.
That's neither a solution nor a good one even if it were. stricmp is typically implemented to match strcmp except with case insensitivity. It's not suitable for a palindrome test, which on top of requiring a reverse direction comparison also has to take zero-weight characters (ie. whitespace) into account.

stricmp is also not a standard function, which means even if it were a viable solution, it's not available on all compilers. You're making an unwarranted assumption about the OP's environment.

Thank you Narue ... now it works ... =D

Thank you very much ... I'll check out what you mentioned unsigned chars later ...

Thx

>use stricmp() to compare the charachter.
That's neither a solution nor a good one even if it were. stricmp is typically implemented to match strcmp except with case insensitivity. It's not suitable for a palindrome test, which on top of requiring a reverse direction comparison also has to take zero-weight characters (ie. whitespace) into account.

stricmp is also not a standard function, which means even if it were a viable solution, it's not available on all compilers. You're making an unwarranted assumption about the OP's environment.

The solution that i gave was not for the Palindrome problem
It was to bypass converting upper and lowercase.

And yes I agree i was ignorant to suggest this function, which is not a standard one.

>The solution that i gave was not for the Palindrome problem
>It was to bypass converting upper and lowercase.

I don't understand. Are you still trying to defend your suggestion? Let's pretend stricmp is portable for a moment. Please show me what solution you were thinking of for testing palindromes that prompted the suggestion of stricmp.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.