Concept of Palindrome, it is a string that will be exactly the same, no matter we read it non-reversed or reversed. Such as SOS, lol, Oppo, SAAS, Madam, RACECAR and EYE.
[Non Reversed] Oppo
[Reversed] Oppo
[Yes, it is Palindrome]
[Non Reversed] Kitchen
[Reversed] nehctik
[No, it is not Palindrome]
C++ Algorithm of Palindrome (Method 1)
How does it work?
Let's see the step of work:
(1) Receive String
(2) Calculate how much "length" of that received string
(3) Detected whether the string is "Even" or "Odd"
(4) So we provided those "length", "Even or Odd" and "Received String" to Computer to analysis by the algorithm.
(5) First we need provided the last number of the length. It is because of starting number must be 0, while the last number is depend on the string we provided, Next is the Odd or Even for PC easy to calculated whether it is palindrome or not.
(6) If It is Even, First do loop
(for int i = 0; i < [length of your string, example 5] - 1; i++){
int start = i ;
int end = 5 - i; (it is because last number will be decrease for each time we compare, same goes to the starting, will be adding after compare)
(7) if(Sample[start] == Sample[end]), Sample is the "received string", but we convert it into "char". So it will be able to analyze and compare easily.
(8) So we know the start will increase, and end will be decrease, so result will be showing like below
Example 1: If we are using Text like "SPIDERMAN"
(8:1) first = S, end = N, and do compare, if (S == N) then continue else it is not palindrome (end)
Example 2: If we are using Text like "ATOMIA"
(8:2:1) first = A, end = A, and do compare, if (A == A) then continue else it is not palindrome (same!)
(8:2:2) first = T, end = I, and do compare if (T == I) then continue else it is not palindrome (end)
(9) For "Odd", which is number like 3,5,7
(10) For "mid" value by using length "divide by" 2.
(11) During comparison in for loop
(12) If first equal to mid then this round should be skipped. Then it will work exactly like "Even".
(1) Receive String
(2) Calculate how much "length" of that received string
(3) Detected whether the string is "Even" or "Odd"
(4) So we provided those "length", "Even or Odd" and "Received String" to Computer to analysis by the algorithm.
(5) First we need provided the last number of the length. It is because of starting number must be 0, while the last number is depend on the string we provided, Next is the Odd or Even for PC easy to calculated whether it is palindrome or not.
(6) If It is Even, First do loop
(for int i = 0; i < [length of your string, example 5] - 1; i++){
int start = i ;
int end = 5 - i; (it is because last number will be decrease for each time we compare, same goes to the starting, will be adding after compare)
(7) if(Sample[start] == Sample[end]), Sample is the "received string", but we convert it into "char". So it will be able to analyze and compare easily.
(8) So we know the start will increase, and end will be decrease, so result will be showing like below
Example 1: If we are using Text like "SPIDERMAN"
(8:1) first = S, end = N, and do compare, if (S == N) then continue else it is not palindrome (end)
Example 2: If we are using Text like "ATOMIA"
(8:2:1) first = A, end = A, and do compare, if (A == A) then continue else it is not palindrome (same!)
(8:2:2) first = T, end = I, and do compare if (T == I) then continue else it is not palindrome (end)
(9) For "Odd", which is number like 3,5,7
(10) For "mid" value by using length "divide by" 2.
(11) During comparison in for loop
(12) If first equal to mid then this round should be skipped. Then it will work exactly like "Even".
No comments:
Post a Comment