Niner since 2004
I would tackle the palindrome problem in this manner. Opinions on the code and complexity are welcome :
Characters, Numbers, Alphanumerics, White Spaces, Wildcards.
Example Test Cases:
tarat, ta r at, tar at, ta 1 at, ab 1A1 ba, ab1A 1ba etc (The code considers them correct inputs).
Ta a, ta 1 ata, etc are not palindromes.
True or False
To acheive efficient memory management, computation time and code complexity, I would parse through the text from the two
corners ie begining and end and reach the center of the text while comparing each of the input characters. There would be
checks for white spaces. Other characters such as commas, appostrophies etc would be considered as character inputs. If it
encounters white spaces, it would ignore them and (increment the begining counter or decrement the end counter).
Other implementation such as a recursive one would require extra stack space and recursive calls to the heap. Which might
not be a good idea. Reversing the string and comparing with the original one would also not be a good idea as extra memory
would be required.
-Initilize all variables ie int beg, end, bool check variables.
-point the beg pointer to the first character of the input string.
-Find length of string and point the 'end pointer' to the (str_len - 1)th position.
-Enter a while loop and compare the beg and end.
---If they are equal, do beg++ and end--.
---if they are not, check for white spaces, ignore white spaces, and inc or dec accordingly ie if beg, inc beg and leave end
as it is, OR, if it is end, than dec end and leave beg to point to where it is pointing.
----if they are still not equal, return FALSE.
-end of program.
The complexity of the code would be O(n) where n is the length of the input string. In reality, the code would run for n/2
iterations as when the beg pointer reaches the middle+1th part of the string, it would stop the while loop.
bool ispalin(char * input)
char * beg=new char;
char * end = new char;
beg = input;
end = &input[strlen(input)-1];
if(beg==0 || beg==NULL)
while(*beg==*end && *beg != input[strlen(input)/2])
if(*beg == ' ')
if(*end = ' ')
char * input = new char;
cin.getline(input, 30, '\n');//as cin.getline caters to white spaces as well
check = ispalin(input);