I currently have a program to detect if a string entered is a palindrome or not. I want to avoid using the strlen method.
int main ()
{
char string[80];
bool Beginning;
Beginning = true;
while (Beginning)
{
cout << "\nEnter a string\n" << endl;
cin.getline(string, 80);
cout << "\nYou entered: ";
printf(string, 80);
cout << endl;
if('E' == string[0])
{
if('N' == string [1])
{
if('D' == string[2])
break;
}
}
int len=strlen(string); //avoid this method
bool flag = true;
for (int c=0; string[c]; c++)
{
if (flag)
{
if(string[c] != string[len-c-1])
{
flag = false;
}
}
else
{
break;
}
}
if (flag)
{
printf ("\nThis is a Palindrome\n");
Beginning = true;
continue;
}
else
{
printf("\nThis is not a palindrome\n");
Beginning = true;
continue;
}
cin.get();
}
}
I have tried manual counting with the following:
while (string[c] < '\0') {
c++;
int len = string[c];
}
The above messes up the programs ability to accurately determine if a string is a palindrome or not, it will say both of them are not, or are.
If I understand correctly you need a correct implementation for len
This will do the job:
len =0;
while (string[++len] );
All you need is to check for null termination (as you're using C-strings )
Suggestion: Please don't mix C & C++ !