### Recursive Palindrome Test with MergeSorting

Hey guys, I'm new here (although I've been lurking through the threads for help the last year or so.) I need help with some of my homework for class and I've got most of it done and its due tomorrow before I go to school so I just need help with a few different things.

Alright, my assignment is to make a program that tests whether something is a Palindrome or not using a recursive function. Also, in order to test what type the Palindrome is (if it is indeed a palindrome) I'll need to merge sort it. Now alphabet characters, spaces, and numbers are all allowed, as long as the spaces line up with the spaces in the original input. And yes this is user inputted. I can show you what I've got so far and then I'll tell you what my problem is.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157`` ``````#include #include #include #include #include using namespace std; /************To Test if Palindrome*******************/ bool IsPalindrome(char *str, int size) { if( str[0] == str[size-1]); { if(size>2) { return IsPalindrome(&str[1],size-2); } return true; } return false; } /*************MergeSorting******************/ void MergeSort(char *str, char *result, int len) { char *str1 = str; int size1 = len/2; char *str2 = &str[size1]; int size2 = len-size1; char result1[256]; char result2[256]; MergeSort(str1,result1,size1); MergeSort(str2,result2,size2); char *c1 = result1; char *c2 = result2; while( size1>0 && size2>0) { if(c1[0]>c2[0]) { *result = *c2; result++; c2++; size2--; } else { *result = *c1; result++; c1++; size1--; } while(size1>0) { *result1=*c1; result++; c1++; size1--; } while(size2>0) { *result2=*c2; result++; c2++; size2--; } *result= '\0'; return; } } /**************Counting the Characters*******************/ int CharCount(char *str) { int i=1; while( str[i] == str[0] ) i++; return i; } /*************Finding Palindrome Type********************/ int PalindromeType(char *str) { if( strlen(str)%2 ) return 2; // test for same number of character of each type int control = CharCount(str); int count = 0; int totalCount = control; while( str[totalCount] != '\0') { count = CharCount(&str[totalCount]); if(count != control) { return 2; } totalCount += count; } // if it fails, type 1 return 1; } /***************Finding Palindrome Order**********************/ int PalindromeOrder(char *str) { for(int i=0; i= 'a' ) return CharCount(&str[i]); } return 0; } /*************Main******************/ void main() { char myStr[128]; //Going to be tested char resStr[128]; //This is the resorted version cout<<"Enter word or phrase to check if is a Palindrome: "; cin>>myStr; //user inputed cout<='A' && myStr[i] <= 'Z' ) myStr[i] += 'a' - 'A'; } /***************Test for Palindrome*******************/ bool test = true; while(test) { IsPalindrome(myStr,myStr[0]); test = false; } /*************If Palindrome or not*******************/ if(IsPalindrome) { cout << myStr << " is a Palindrome!\n"; int type = PalindromeType(resStr); cout << "Palindrome type is " << type << endl; int order = PalindromeOrder(resStr); cout << "Palindrome order is " << order << endl; } else { cout << myStr << " is NOT a Palindrome!\n"; cout << "Not a Palindrome - Type not relevant!\n"; cout << "Not a Palindrome - Order not relevant!\n"; } return; } ``````

Now my problem is that I can't seem to get the test to work correctly. Since the word is user inputted I don't have any way of knowing the size so that kind of complicates it a little bit. The other thing is I was told by my professor that I need a boolean flag to make it work, and I need to set the value to the return of the IsPalindrome function. I'm not sure how to do that. I also don't haven't been able to add in the MergeSort yet, so the Order and Type don't really work correctly because I'm not sure how to get `int len` for it. (len is length)

I did manage to fix one part of it, and its the test for the palindrome that said:
 ``123456`` `````` bool test = true; while(test) { IsPalindrome(myStr,myStr[0]); test = false; }``````

I changed it to:
`bool test = IsPalindrome(myStr,strlen(myStr));`
Is the merge sort part of the assignment requirements?

Why not use strings instead of char* to deal with length problems?
Last edited on
Yeah the merge sort is part of the assignment requirements. I actually thought about changing char to strings because of the fact that it would fix my length problem, but after we (meaning the professor to the class) wrote it as char* in the demonstration I had written it all with that and I didn't want to go through and change every instance of it to string and was determined to find a way to do it without having to do that but it looks like I might have to. Maybe the professor did that on purpose to see how many of us were actually smart enough to change it or something? Crossed my mind a couple times and wasn't too happy about it. Hope it won't take as long as I'm making it out to be.

Thanks for the input.
Topic archived. No new replies allowed.