Purchase Solution

Algorithm and Pseudo-Code

Not what you're looking for?

Ask Custom Question

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

Attachments
Purchase this Solution

Purchase this Solution


Free BrainMass Quizzes
Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Basic Networking Questions

This quiz consists of some basic networking questions.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.