it's given a string S with n characters placed in an array.Build an algorithm that turns as an answer True if the string is Palindrome and False if it isn't (palindrome-a word, reading the same backward as forward,)
Is it pseudo code? If so: Let S be a string Let AuxS be an empty string For character in S: Append character at the beginning of AuxS #If you have indexed access to string, to #access it backwards, like S[-1], S[-2] etc If AuxS == S: Return True Else: Return False In python code: http://codepad.org/Tw5jTg4U
Damn, sorry for that broken commentary in the middle of the answer, OS is sluggish for me, wrote twice. But I think that it is comprehensible :-)
well i wanted it in pascal if you don't know pascal can you explain with words and what does this part mean Append character at the beginning of AuxS #If you have indexed access to string, to #access it backwards, like S[-1], S[-2] etc... because i'm not getting either this auxS for what reason is created
In Pascal, you have indexed access to an array (that's what I meant by my commentary). If you have access with indexed character, you can do it without creating an auxiliary array (I was reversing the array using an empty array). You might want to do it like this: Let S be a string. Let i, j be integers. i = 0, j = length(S) Let isPal = True. While (i < j): if S[i] != S[j]: isPal = False break else: i = i + 1 j = j - 1 return isPal The algorithm here is like this: You set to values, and access the array with them. One of them is at the beginning of your array, and the other starts at the end of your array. It will compare each letter at the beginning with its corresponding in the end of the array (first letter with last letter, second letter with the second-to-last latter, etc). If this comparison returns false, i.e. the second letter is different from the second-to-last, you know that the word is not a palindrome, and you set isPal - a boolean variable - to false and break out of the loop. Else, you increment the beginning value by 1 and decrement the end value by 1. There will be a point where i == j, that is, the middle of the array. As the indexes are the same, the letter is the same (S[2] == S[2]), so if it reaches that case, the word is a palindrome, and isPal will return True.
Join our real-time social learning platform and learn together with your friends!