Case-insensitive palindrome checker (help!!)

Hello all, I am currently being driven insane by a COSC class that seems to have a large disparity between programming assignments. Prior to this assignment our programs only needed basic algebraic operators to pass the required checks. Now I have gotten completely lost and I can no longer tell what I am doing wrong here and now my code probably looks like an utter mess after having chopped it up multiple times.

My problem, because I thought this might be the simplest solution, is I cannot figure out how to use a tolower() or toupper() function to set an input to all lower or uppercase before my boolean runs its' string check. My assignment is due tomorrow and I have been at this for days, I even had 3 hours of tutor help from my school and they couldn't get it to work. The requirements here are I must modify the provided boolean function which is the first code I will post.

1
2
3
4
5
6
7
8
9
10
bool isPalindrome(string str)
{
int length = str.length();
for (int i = 0; i < length / 2;i++) {
    if (str[i] != str[length - 1 - i]) {
         return false;
    } // if
  } // for loop
 return true;
}// isPalindrome 



And here is my mess, my only victory is I have successfully gotten a while function to not cause the compiler to throw out fatal errors. I am now near meltdown point as I feel like I am running out of avenues to try.


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
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <algorithm>
#include <cctype>
#include <string>

using namespace std;

bool isPalindrome(string str) 
{
int length = str.length(); 
for (int i = 0; i < length / 2;i++) {
    
    char c;
  while (str[i])
  {
    c=str[i];
    putchar (tolower(i));
    i++;}
    return i;  
    if (str[i] != str[length - 1 - i]) {
         return false;
    } // if    
  } // for loop
 return true;
}// isPalindrome
    int main() {
      string Word;
  cout << "Enter object for analysis: " << endl;
  cin >> Word;
  if(isPalindrome(Word))
  cout << Word <<" is a palindrome.";
    else cout << Word <<" is not a palindrome.";
      return 0;
}


Thank you for reading!
Last edited on
you can use transform() to set all the letters of a string upper or lower.

Stolen from google search:
1
2
3
std::string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(),
    [](unsigned char c){ return std::tolower(c); });


there is no built in way to do an entire string; the C++ language folks probably thought we could manage a loop for ourselves on this one, though they are really weird about when they loop for us and when they make us DIY.
Last edited on
Whoa that was an insanely quick reply. This is my first class and only the 6th week of it. I have seen this code before but I only vaguely understand it and not sure where to put it. Can it just go into int main () after the cin >> inputstring or should it go into the boolean itself before the length check? *trying both now*
Last edited on
you can put it wherever you want to change the string's case. main is ok, or inside some other function is ok, or its own function.

its gibberish code to be sure. what it says..
create an un-named mini-function (called a lambda) indicated by the [] which calls tolower on a single character.
transform says to apply that lambda to each letter of the string, from its begin() to its end().
Begin and end are 'iterators' and all the c++ containers have them to allow you to ... iterate ... over the data one by one.

its exactly the same, by the way, as just saying
for(int i = 0; i < data.length(); i++)
data[i] = tolower(data[i]);
and honestly the above is easier to read, but the language has moved on from easy to read into the realm of hand waving so you may as well get used to it now :)
Last edited on
I feel like a complete failure. I can only get it to set the first character to lowercase. I'm confused out of my mind right now.


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
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <algorithm>
#include <cctype>
#include <string>

using namespace std;

bool isPalindrome(string str) 
{
  
int length = str.length(); 
for (int i = 0; i < length / 2;i++) 
{ 
  str[i] = tolower(str[i]);
    if (str[i] != str[length - 1 - i])
     {
         return false;
    } // if    
  } // for loop
 return true;
}// isPalindrome
    int main() {
      string Word;
  cin >> Word;
  if(isPalindrome(Word))
  cout << Word <<" is a palindrome.";
    else cout << Word <<" is not a palindrome.";
      return 0;
}


something like FFFFffff with correctly output as a palindrome, however FFffFFff will not
Last edited on
The checker is also looking for some weird code I don't understand.

First check successfully finds

\s\isPalindrome\(string str\)

2nd-6th checks fail to find

.+*isPalindrome\(\"abBa\"\).+*
.+*isPalindrome\(\"22\"\).+*
.+*isPalindrome\(\"67876\"\).+*
.+*isPalindrome\(\"444244\"\).+*
.+*isPalindrome\(\"trYmeuemyRT\"\).+*

On top of this the outputs from my program match the expected outputs from the checker. WHAT IS HAPPENING??
Last edited on
Hello Icebrand,

Try this:
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
#include <iostream>
//#include <ctype.h>  // <--- C header file.
//#include <stdio.h>  // <--- Should be covered under "iostream".
//#include <algorithm>
#include <cctype>  // <--- C++ header and what should be used.
#include <string>

using namespace std;

bool isPalindrome(string& str)
{
    for (size_t idx = 0; idx < str.size(); idx++)
    {
        str[idx] = std::tolower(str[idx]);
    }

    int length = str.length();
    
    for (int i = 0; i < length / 2; i++)
    {
        //char c;

        //while (str[i])
        //{
        //    c = str[i];
        //    putchar(tolower(i));  // <--- "putchar(std::tolower(str[i]));" or "putchar(std::tolower(c));"
        //    i++;
        //}

        //return i;  // <--- Returning to soon and the wrong variable.

        if (str[i] != str[length - 1 - i])
        {
            return false;
        } // if    
    } // for loop

    return true;
}// isPalindrome

int main()
{
    string Word;

    cout << "Enter object for analysis: ";
    cin >> Word;

    if (isPalindrome(Word))
        cout << '\n' << Word << " is a palindrome.";
    else
        cout << '\n' << Word << " is not a palindrome.";

    return 0;
}

The while loop in the function locked up my computer and didnot appear to be needed.

Andy
your code, properly indented
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
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <algorithm>
#include <cctype>
#include <string>

using namespace std;

bool isPalindrome(string str) {
	int length = str.length();
	for(int i = 0; i < length / 2; i++) {
		str[i] = tolower(str[i]);
		if(str[i] != str[length - 1 - i]) {
			return false;
		}
	}
	return true;
}

int main() {
	string Word;
	cin >> Word;
	if(isPalindrome(Word))
		cout << Word << " is a palindrome.";
	else
		cout << Word << " is not a palindrome.";
	return 0;
}
your problem is that you did lowercase str[i], but didn't touch str[length - 1 - i] yet
so do one thing at the time, first convert at lowercase, then check if it is a palindrome
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
string tolower(string str){
	for(int i = 0; i < str.length(); i++)
		str[i] = tolower(str[i]);
	return str;
}

bool isPalindrome(string str) {
	int length = str.length();
	for(int i = 0; i < length / 2; i++) {
		if(str[i] != str[length - 1 - i]) {
			return false;
		}
	}
	return true;
}

int main() {
	string Word;
	cin >> Word;
	if(isPalindrome(tolower(Word)))
		cout << Word << " is a palindrome."; //note that it will print the original version, not the lowercase
	else
		cout << Word << " is not a palindrome.";
	return 0;
}

exercise for the reader: modify the code so it checks for palindrome ignoring whitespace
to read a line including whitespace, may use
1
2
string line;
getline(cin, line);

jonnin, Handy Andy, ne555, you guys are rockstars. Between the three of you I was able to put together a program that worked.

MindTap still hates it. It is looking for stuff that isn't there.

.+*isPalindrome\(\"abBa\"\).+*
.+*isPalindrome\(\"22\"\).+*
.+*isPalindrome\(\"67876\"\).+*
.+*isPalindrome\(\"444244\"\).+*
.+*isPalindrome\(\"trYmeuemyRT\"\).+*

I don't recognize these strings from anywhere in the book, but the program now works flawlessly regardless of how complex of a palindrome I input. Mind you this is entry-level COSC-1436 Programming Fundamentals I. I put an absolutely absurd amount of effort into researching this and I am just going to approach my professor with the cpp file if necessary.

Again, thank you guys so much!!
If all you want is to make the Palindrome check case-insensitive, then:

1
2
3
4
5
6
7
8
9
10
bool isPalindrome(string str)
{
int length = str.length();
for (int i = 0; i < length / 2;i++) {
    if (tolower(str[i]) != tolower(str[length - 1 - i])) {
         return false;
    } // if
  } // for loop
 return true;
}// isPalindrome  

Topic archived. No new replies allowed.