toupper();

I have a palindrome function that returns true if a word can be spelled the same way backwards as it is forward. The function works but I need to make it so it is not case sensitive. So, aBbA should return true. I have tried using the toupper function to correct this and it is not working. I put the toupper() in a cout statement to observe its value and it looks like its giving me an address instead of the char values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  bool isPalindrome(const char* inString)
{
	char* start = (char*)inString; // assign 1st address of inString to start and truncate const
	char* end = start + strlen(inString) - 1;// assign 1st address + last address to end

	
	while (start < end) { // ist address < last address
		cout << toupper(*inString);
		if (*start != *end) // 1st char != last char as they increment toward each other.
			return false;

		start++; // increment to next address
		end--;   // decrement to next address 
	}
	return true;
}


I have tried using an additional while loop before the first one and this didn't work either.
1
2
3
4
5
while (*inString != NULL) {
		toupper(*inString);

		inString++;
	}


I figured it out.
1
2
3
4
while (*start != NULL) {
		*start = toupper(*start);
		start++;
	}
Last edited on
stoneJax wrote:
I put the toupper() in a cout statement to observe its value and it looks like its giving me an address instead of the char values.
address or integer?
Without using manipulators addresses are displayed in hexadecimal.

By the way, why does toupper() accept and return 'int'? It's meant for 'char' after all..
Last edited on
This line is pointless - why write out the same thing each pass through the loop?
cout << toupper(*inString);



Try
if (toupper(*start) != toupper(*end))
Topic archived. No new replies allowed.