toupper

I have created a function that takes an array of chars, and two arguments as bounds for indices that are to be checked if a word is a palindrome (spelled the same way forwards as it is backwards). The function works and now I have to make further modifications. For example, it should not be case sensitive, so "aBbA" -> "abba" -> "ABBA" should all test true for palindrome. So, I have used the toupper function to convert everything to uppercase and for some reason this line of code has a green squiggly line under it that says "return value ignored." I am using Visual Studio, and I have used toupper before with the same headers without problems so I cannot see what is wrong here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

bool isAPalindrome(char *inString, const char* start, const char* end);

int main()
{
	const int SIZE = 256;
	char inString[SIZE]{};
	int first, second;

	while (1) {

		cout << "Enter a string: ";
		cin.getline(inString, SIZE);

		cout << inString;
		if (isAPalindrome(inString, &inString[0], &inString[strlen(inString) - 1])) {
			cout << " is a palindrome" << endl;
		}
		else cout << " is not a palindrome." << endl;



		cout << "Pick 2 elements, everthing in between "
			<< "and including the elements wil be checked for palindrome." << endl;
		cout << "1st element: ";
		cin >> first;
		cout << "2nd element: ";
		cin >> second;
		if (isAPalindrome(inString, &inString[first], &inString[second])) {
			for (first; first <= second; first++) {
				cout << inString[first]; 
			}
			cout << " is a palindrome" << endl;
		}
		else {
			for (first; first <= second; first++) {
				cout << inString[first];
			}
			cout << " is not a palindrome." << endl;
		}
			
			
		cin.ignore();
	}

	system("pause");
	return 0;
}




bool isAPalindrome(char *inString, const char* start, const char* end)
{
	if (start < end) {
	
		if (*start != *end) 
			return false;

			toupper(*inString);   // Green squiggly line here
		    isAPalindrome(inString, start+1, end-1);
	}
	return true;
}
Last edited on
toupper doesn't change what you pass in. toupper RETURNS the upper case version. Your line 65 does nothing.
How would I use it in this scenario though? I tried passing it as an argument but that didn't work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cctype>
#include <iomanip>

// note: closed interval [first,last]
bool is_palindrome( const char* first, const char* last ) // note: const
{
    if( last <= first ) return true ;

    // skip non-alphanumeric characters
    if( !std::isalnum(*first) ) return is_palindrome( first+1, last ) ;
    else if( !std::isalnum(*last) ) return is_palindrome( first, last-1 ) ;

    // both alphanumeric: compare ignoring case (convert both to upper case and compare)
    if( std::toupper(*first) != std::toupper(*last) ) return false ;

    return is_palindrome( first+1, last-1 ) ;
}

int main()
{
    const char test[] = "A man, a plan, a canal - Panama!" ;
    const char* first = test ;
    const char* last = test + sizeof(test)-1 ;

    if( is_palindrome(first,last) )
        std::cout << "the string " << std::quoted(test) << " is a palindrome\n" ;
}

http://coliru.stacked-crooked.com/a/7f96a5094533f839
I can see it with the pointers in the if statement. That makes it work. I haven't used toupper like that before, that was helpful.

Thank you!
Topic archived. No new replies allowed.