The string reversal algorithms is one of the most common introductory code challenges. In this section, we will explore various string reversal techniques
Write a method that will take a string as input, and return a new string with the same letters in reverse order without using the built-in #reverse method
1) While loop
2) #each enumerator
Before diving into the code, it is best to process your logic using pseudocode to solve the problem. In other words, what are the sequence steps, in plain English, that you will need in order to take a string of letter as an input and output that string in reverse order?
- String is input to the method
- Create an empty string called 'string_reversed' in which will hold the result
- Find access all of the characters in the string
- Put the characters from the string into ‘reversed_string’, one at a time
- With each iteration, put each character IN FRONT OF the character currently in the string (for example if ‘reversed_string’ currently holds ‘a’, when we put in the next letter, we want ‘reversed_string’ to hold ‘ba’…)
- Return my answer — ‘reversed_string’
- Split the input string to an array of single letter and lowercase each one
- Create an empty array called 'string_reverse'
- Use the #each enumerator to traverse through each 'char'
- Within each iteration, use #unshift method to prepend that first character of input string into 'string_reversed'
- Call the #join method to convert the array back to a string
Our next challenge is build on top on using the #reverse_string and further strengthen our string manipulation skills by implementing simple algorithms to test if a string of text is a palindrome.
First off, what is a palindrome?
A palindrome is a word, number, or other sequence of characters which reads the same backward as forward, such as "racecar" or tacocat"
- An Intuitive Approach
- Looping Through and Comparing Characters
Write a method that takes a string and returns true if it is a palindrome.
- Create a conditional statement to test if the input string matches with invoking the string of the #reversed_string method
- Return true if it does
- Otherwise return a string stating it's not a palindrome
In this approach, we are going to apply the Two Pointers Technique in which loops through the string as it was passed in and compare each character with the character currently in the position it'd have taken if the string was reversed.
For instance, if we were testing the string "coding", we would compare "c" with "g" because if the string was reversed d would take r's position.
Correspondingly, we'd compare "o" in position 2 with "n" in position 2 from the end as well and if the string were a palindrome, all of these would test true.
- Lowercase and split the input string into an array; assign that to 'char_array'
- Traverse through each character and index to check if that character matches with the position if it was going backwards.
- Return false immediately if it does not match
- Otherwise, return true
In our third challenge, we are going to continue to manipulate arrays and strings to count the number of vowels in a given string of text.
- While loop
- #each enumerable
Write a method that takes a string and returns the number of vowels# in the string. For example:
- Create 'vowels' and 'counter" variables and set to zero
- Use a while loop to check if each character matches with a vowel
- If it does, increment the "vowels" to 1
- Once out of the conditional statement inside the while loop, increment the counter by 1 to move onto the next character
- At the end, return the 'vowels' variable
- Create an array of vowels; assign it to a 'vowels' variable
- Create a counter to keep track of the number of vowels
- Iterate over the string using #each
- Within each pass, check if the current character matches with the vowels array
- if it does, increment the counter variable by 1
- In the end, return the counter variable
Stay tune for more algorithm challenges in part 2!