Urgent! Need help finding algorithm - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Urgent! Need help finding algorithm

Posts: 1Member
I have an assignment about finding the algorithm of carcinoid phrases like "Anna" or something like "Pollop", words that can be spelled the same backwards. I have created one of my one but it is too scratchy for my proffesor. If anyone can help sending an URL please do, i need it yesterday!
I also need additional information about these phrases. I have been over 6 hours on search engines and i have found nothing. I need help finding resources so if anyone can help i would appreciate it very much )
Thank you

• Posts: 272Member
The words you are referring to are called palindroms. I assume you are trying to determine if a given string is a palindrom. To do this, you create a string that is the reverse of the other, set the case on both of them to be the same, and use string.equal(string) to see if they are the same. To reverse the string, simply loop through the string one character at a time and place it at the beginning of a temporary string, such as:

[code]
while (i < str1.length())
str2 = str1.charAt(i) + str2;
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
return str1.equals(str2);
[/code]

I wrote this code using Java, which has these methods at my disposal. If you don't have these methods, then you would have to parse the original string one character at a time, and then store the character at the beginning of one string and at the end of another:

[code]
while (myStr != null)
{ Char c = myStr.substring(0,1);
myStr = myStr.subString(1);
str1 = str1 + c;
str2 = c + str2;
}
while (str1 != null)
{ Char c1 = str1.substring(0,1);
Char c2 = str2.substring(0,1);
str1 = str1.substring(1);
str2 = str2.substring(1);
if (c1 != c2)
{ retValue = false;
str1 = null;
}
}
return retValue;
[/code]

I can't think of any other way to do this right now, but if one comes to me, I'll let you know.

If you have enough memory, you won't have enough disk space.

• Posts: 3Member
There is a website that has weekly programming competitions that was mentioned elsewhere that has an algo for finding palindromes.

http://www.geocities.com/acmesofties/

All code in C

[CODE]
int ispalin(char *a,int st,int end)
{
return (st>=end)?1:(a[st]==a[end])?ispalin(a,st+1,end-1):0;
}
[/CODE]

But you could also try this:

[CODE]
int IsPalin(char *s)
{
int i, j = 0;
for(i = 0; s[i]; i++) ;
while(s[--i] == s[j] && i > ++j) ;
return j >= i;
}
[/CODE]