the first letter of the output string will be the first letter of the first word of the sentence.
3 views (last 30 days)
Show older comments
Whose input is a sentence (as a string). The words of the sentence are separated by single blanks, and there is a dot at the end of the sentence. The output will be a word (as a string) created by alternatingly picking the first and last letters of the words of the sentence:
The first letter of the output string will be the first letter of the first word of the sentence. The second letter of the output string will be the last letter of the second word. The third letter will be the first letter of the third word, and so on. (You may NOT use any built-in functions except size, length, rem and mod)
Example:
If Sentence= 'The tree nut sings in no need.'
and Out = first_last_letters (Sentence)
Out will be Out='Tension'
Accepted Answer
Rohith Nomula
on 9 Jun 2020
Edited: Rohith Nomula
on 9 Jun 2020
You can split your string using a delimiter. which in your case is space
new_s = split(s,' ')
create a dummy string ans and append the required fields
Run a loop on new_s so that you can access first and last chars as follows
% for odd index string
ans = append(ans,new_s{i}(1))
% for even index string
ans = append(ans,new_s{i}(end))
Try this !
3 Comments
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!