Explore BrainMass
Share

# Algorithm and Pseudo-Code

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

Quest:
In English like pseudo-code, or structured English -- just to make sure everyone can read it; write an algorithm to determine if a string is a palindrome. A palindrome is a word or phrase that is spelled the same whether you are reading it forwards or backwards (ex. race car, Madam I'm Adam). Your algorithm should ignore spaces and punctuation. Make sure to clearly state your assumptions before presenting your answer and to justify your use of data structure: what data structure did you choose (from lists, stacks, queues or trees) and why, and briefly explain why you did not choose the others
(CAN YOU JUST CHECK IF THE BELOW GIVEN 2 ALGORITHMS ARE CORRECT OR IS THERE SOME CHANGE THAT CAN BE MADE AND EXPLAIN WHY)

Sol 01
Procedure determinePalindrome ( st as string)
{
/* ignoring spaces and punctuation */
Declare a list variable to store the spaces and punctuation ignored string. List[ ]
len= length of st
Dynamically make the list variable size as length of the string
Declare i as integer and j as integer
Initialize k value as 1
Initialize j value as 1
Loop
{
/* get j th position character from the st . i.e if j=1 1st character, j=2 2nd character and so on*/

If character is not a space and character not is not a punctuation then
{
Add the character to the list
List[k] = character
Increment k by 1
}
Increment j by 1
} continue until j < = len

/* determining string is a palindrome or not */

/* here length of the new string is k -1 */
Declare variable x as integer
Initialize x with floor(k/2) /* floor is rounding the fractions down*/
Assign j value as 1
Decrement k value by 1 /* now this the length of the new string*/
Loop
{
/*Get the j th postion character and k th character from the list*/
If list[x] <> list[k] then
Display as the given string is not a palindrome
Exit procedure
End if
Increment j by 1
Decrement k by 1
} continue until j <=x

Display as Given String is a palindrome.

}

Sol 2
//In this procedure '=' refers to the assignment statement and '==' refers to the equal statement
//The array starts with '0' as the first pointer instead of '1'
procedure checkPalindrome (string inputString)

if the length of inputString is less than 2 characters
then
return error "String too short"
end if

int i, j, k
char stringArray[length of inputString]

i = 0
for (i < length of inputString)
do
{stringArray[i] = character i of inputString
i = i + 1
end for

i = 0
j = length of stringArray - 1
k = 0

while (j - i >= 0) and (i <> j) and (k == 0)
do
if (stringArray[i] is a space or punctutation)
then {i = i + 1
}
else {
if (stringArray[j] is a space or punctuation)
then {j = j - 1
}
else {
if (stringArray[i] == stringArray[j])
then {
i = i + 1
j = j + 1
}
else { k == 1}
end if
end if
endif
end do

if (k == 0)
then {report inputString is a palindrome
}
else {report inputString is not a palindrome
}
end if

end procedure

https://brainmass.com/computer-science/arrays/algorithm-pseudo-code-94175

\$2.19