I haven't programmed in a while, but couldnt' the logic be:
1. Ensure all values are proper (not nulls, right datatypes, etc)
2. Read through string forwards
3. Reverse string
4. If both are identical, you've got your palindrome
I'd do a different algorithm to allow for more flexibility (to allow for different collations, for example)
1) Find or write a string_reverse() function that would take a string and return it's reverse (for example, string_reverse("ABCDE") would be "EDCBA") - this would have to handle Unicode data intelligently as the byte-reverse order of a set of bytes is not the same as the character-reverse order of a Unicode string. UTF-8 is also different from UTF-16 or UTF-32. If the data is guaranteed US-ASCII this is not a problem.
2) write is_palindrome(s) as follows:
Count the number of characters (not bytes) in the string using a string_length function
Create four local string variables s1, s2, s3, and s4
s1 <= Make a copy of s
Strip all palindrome-insignificant characters from s1 (punctuation, whitespace, etc.) Perhaps it would be easier to define palindrome-significant characters (A-Z, sure. 0-9? _? "-"? Are accented characters to be replaced with their vanilla equivalents? Perhaps preserve accented characters and let string_compare handle that.)
Canonicalize case of s1 as appropriate - all upper-case or lower-case
If the number of characters in the string is odd:
* Set s1 = the first half of the string, NOT including the middle character
* Set s2 = the second half of the string, also NOT including the middle character.
* Note if s was a single character (smallest positive odd number), both s1 and s2 are the empty string.
Otherwise I can safely assume the number of characters in the string is even (perhaps 0)
* Set s1 = the first half of the string
* Set s2 = the second half of the string
Here the branches join again.
Set s3 = string_reverse(s2)
if s2 and s3 are "the same" (with respect to collation) then return TRUE
otherwise return FALSE
* Create a copy s3 = string_reverse(s2)
* if s2 and s3 are "the same" (with respect to collation) then return TRUE
* otherwise return FALSE
2007-01-23 09:16:03
·
answer #1
·
answered by Sabine 6
·
1⤊
0⤋
The good thing about strings in C is that they are represented as an array of characters. You could run a loop reversing the characters of the string and store it in another. After that, you could use
if(!strcmp(x,y)) ...
Another method is using a loop to compare the first and last characters until you get to the middle...
int end, boolSame, iter;
char myString[] = "whatever word...";
// use a scanf() to get your string from the user
iter = 1;
end = strlen(myString);
for(iter = 0; iter < (int)(end / 2); iter++)
{
// store 0 if different
if(myString[iter] != myString[end - iter])
{
boolSame = 0;
break;
}
}
if(boolSame) printf("Palindrome"); else printf("Not palindrome");
2007-01-23 09:16:21
·
answer #2
·
answered by headaches abound 2
·
0⤊
0⤋
The concept is simple enough.
You start with a String STR
You build a second string starting with the BACK of the first one
RST
Then you compare STR and RST
All you need is the code for building the second string. I am not familiar with the syntax for C, but it would be something like
RST = ''
StrLen = length(STR)
For i := StrLen - 1 to 0, i-- do
RST = RST + STR[StrLen]
If STR = RST return true
else return False
2007-01-23 08:56:52
·
answer #3
·
answered by diogenese19348 6
·
0⤊
0⤋
You can implement this in several ways. One way is to compare the first element in the string with the last element...if they match, then check the i+1, n-1 element, if they match ...
Another way is to reverse the string into another variable and just do a strcmp on it
2007-01-23 08:53:12
·
answer #4
·
answered by mdigitale 7
·
0⤊
0⤋