Can anybody see what is wrong with this? It gets stuck in a loop. I think I am too tired!! Thanks!
Code:
#include <iostream.h>
#include <string.h> //used for strlen
bool InLanguage(char s[], int first, int last)
{
// Even length -> false
if((last-first)%2 == 1)
return false;
char sf=s[first],
sl=s[last];
// Both MUST be equal, otherwise false.
if(sf != sl)
return false;
// Since sf==sl, check sf only
if(sf != 'a' && sf != 'b')
return false;
// check single char sequence first
if (first == last)
return true;
else
return InLanguage(s, first + 1, last - 1);
}
void main()
{
char string[20];
unsigned int First, Last;
cout << "Enter a string less than 20 characters long: ";//prompt
cin >> string;
do
{
cout << endl;
cout << "Enter first index: ";//prompt user for input
cin >> First; //input
cout << "Enter last index: ";//prompt user for input
cin >> Last; //input
} while((First < 0 || First > strlen(string)-1) || (Last <= First ||
Last > strlen(string)-1));
for(unsigned int a = First; a <= Last; a++)
{cout << string[a];}
if(InLanguage(string, First, Last))
{cout << " is in the language." << endl;}
else
{cout << " is not in the language." << endl;}
}