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.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <istream>
#include <string>
#include <string.h>
#include <conio.h>


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<strlen(str); i++)
	{
		if( str[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<<endl;

	/*************Makes it non Case-Sensitive****************/
	for(int i=0; i<strlen(myStr); i++)
	{
		if( myStr[i]>='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)

Thanks in advance!
I did manage to fix one part of it, and its the test for the palindrome that said:
1
2
3
4
5
6
 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.