Manipulating a string in MATLAB.
So, after the previous post, my colleagues at work gave me a new challenge to do in Matlab. The problem statement was :Matlab is given a string (aka statement) which is a general combination of a lot of vowels and constants that make up words. Say for example like this:
monkeys like eating bananas that are ripe and yellow.
Assuming all the letters are in small ( program can be edited to have letter sin both caps and small as well) , the program reads through the statement and replace the last vowel in every word with nothing , or just truncates it. To add to the challenge the solution must be done without using an inbuilt library functionality.
Therefore after doing what has been asked for, the final output should be this :
monkys lik eatng banans tht ar rip nd yellw.
So, attempted to solve this using matlab and you can find the code here:
The way I solved is this. I am sure there can be several other ways to do that, and I am looking forward to hear from you in the comments section.
- Add a space to the end of the string.
- Find the number of spacer by iterating through the string and checking for integer value of each character. The ASCII value of space is 32.
- The number of space is equal to the number of words then. Since we have added an extra space to the end.
- Now ask matlab to loop through each word. This can be done by space locations and first character. It can be made simpler by adding a space to the beginning of the statement as well. Then the whole string is homogeneously spaced.
- Fing the index of vowels in each word.
- list the index of each vowel
- Remove it by using a logical indexing operation.
- Print it.
- And you are done !
Let me know about your algorithm in the comment sections.