Case Insensitive Palindrome

I am making a case insensitive palindrome but it is only working on lower cases letters like racecar and madam but, if you put RaCeCar, it is not working. Any help is appreciated!

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
void palindrome(char sal[120])
{
	int i, length;	
    int palindrome = 0;
    
    cout << "Enter a string: "; 
	cin >> sal;
    
    length = strlen(sal);
    
    for(i=0;i < length ;i++){
        if(sal[i] != sal[length-i-1])
		{
            palindrome = 1;
            break;
   		}
	}
	cout<<"In reverse order: "<<strrev(sal) << endl;
    
    if (palindrome) {
        cout << sal << " is not a palindrome" << endl; 
    }    
    else {
        cout << sal << " is a palindrome" << endl; 
    }
  
}
Why is sal passed as a parameter and it's value not used?

The use of palindrome seems to be backwards - wouldn't it make more sense to be 1 for is and 0 for not?

strrev() isn't a standard function.

 
cin >> sal;


where sal is a char array (c-style string), don't do this as it can cause a buffer overlow. Either specify a size or use std::string (preferable).

Consider:

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
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;

void palindrome()
{
	char sal[120] {};
	bool ispal {true};

	cout << "Enter a string: ";
	cin >> setw(120) >> sal;

	for (size_t i = 0, length = strlen(sal); i < length; ++i)
		if (tolower(sal[i]) != tolower(sal[length - i - 1])) {
			ispal = false;
			break;
		}

	cout << "In reverse order: " << _strrev(sal) << '\n';

	if (ispal)
		cout << sal << " is a palindrome\n";
	else
		cout << sal << " is not a palindrome\n";
}

int main()
{
	palindrome();
}

Hello WeebTriestoCode,

My first questions are why the "char" array? And why are you defining the variable in the parameter list just to overwrite whatever you may have sent to the function?

Next question is why not a "std::string"?

markyrocks has given you the link to what you need. You could incorporate that in your existing for loop or do something like this:

1
2
3
4
for (int idx = 0; idx < sal.size(); idx++)
{
    sal[idx] = std::tolower(sal[idx]);
}

This is using a "std::string", but can easily be changed to use the "char" array if you really need it.

This is more an example to show you how it works because you can easily use the "std::tolower()" in the if statement and change the case before the compare.

Andy
Hello WeebTriestoCode,

After I posted and saw seeplys's responce I realized I should have mentioned.

When posting code it is usually best to post a complete program that can be compiled and tested.

This way anyone reading your code will be able to see what you have done and may even see a potential problem that has not shown up yet.

Also people do not have to guess at what you might have done which could mean that their version could be completely different.

In the future you will be posting code that uses an input file. You need to include this input file in your post. If it is large than a good sample will work, say 5 to 10 records. And if there is any line in the input file that is a problem you will need to include this in the sample.

Andy
Topic archived. No new replies allowed.