#include <cstring>
#include <iostream>
#define FALSE 0
#define TRUE 1
#define PALIN 1
#define DPALIN 2
#define IPALIN 3
#define NONE 0
usingnamespace std;
void Turn_Round(char *Word, char *(&Return))
{
int Size_W = strlen(Word);
int Size_R = strlen(Return);
int count = 0;
for(int i = Size_W - 1; i > -1; i--)
{
Return[count] = Word[i];
count++;
}
}
int is_equal(char *W1, char *W2)
{
int S1 = strlen(W1);
int S2 = strlen(W2);
if(S1 != S2) return FALSE;
int count = 0;
for(int i = 0; i < S1; i++)
{
if(W1[i] != W2[i])
{
count++;
break;
}
}
if(count > 0) return FALSE;
return TRUE;
}
int Palin(char *Word)
{
cout << " > The word is : " << Word << endl;
char *Use;
Turn_Round(Word, Use);
cout << " > The word turned around is : " << Use << endl;
if(is_equal(Word, Use))
{
cout << " > The word is a palindrome...\n";
return PALIN;
}
else
{
cout << " > The word is not a palindrome...\n";
}
//char *Temp;
return NONE;
}
int main(int argc, char *argv[])
{
char Word[20];
cout << " > Insert a word : ";
cin >> Word;
int i = Palin(Word);
return 0;
}
The thing is that the line commented out in the function Palin, the "char *Temp;" causes a segmentation fault. I need to create a new char* to keep checking for other stuffs in the string, I thought the segmentation fault was because I was initializing a pointer and then I didn't use it, but even if I initialize the pointer, or use it, it keeps giving the segmentation fault!
It is surely a really simple error, but I'm not a pretty much experienced programmer, so I can't find out what it is. Thanks! And sorry for my bad english haha.
You could avoid the C style code and make things a lot simpler:
1 2 3 4 5 6 7 8 9 10
bool isPalindrome(const std::string& s) /* notice that C++ has a built-in type bool,
so there's no need for macro hacks */
{
std::string temp;
for(unsignedint i = 0; i < s.size(); ++i)
{
temp += s[s.size() - 1 - i];
}
return s == temp;
}
You could simplify that even more with a standard library function, but I guess that would defeat the purpose of the exercise.