# How do you capitalize the first and the third letter of each word in a sentence?

3 views (last 30 days)
Tatiana Kobachishvili on 21 Sep 2021
Commented: Cris LaPierre on 21 Sep 2021
How do I capitalizde the first and the third for in a sentence using functions?
Example: my pet is hungry
To >> My PeT Is HuNgry
John D'Errico on 21 Sep 2021
Seems pretty easy.
1. Find the first letter of each word.
2. Capitalize it.
3. Determine the length of each word, by finding the LAST letter of each word.
4. If any word has length at least 3 letter, then capitalize the letter 2 after the first letter in that word.
All of the above is probably even doable using a vectorized set of operations if you are careful.
What is the problem? This seems trivial, at least at the most simplistic level.
You may need to be careful, in how you watch for the inter-word breaks. Is it only spaces that you need to look for, or is punctuation expected? How about a tab, or other classes of white space? Carriage returns? What about a comma, period, or semi-colon, exclamation mark, even a question mark? How about a word like "don't" where not all characters are letters? That might not be too bad, but then how about a shorter contraction like "I'll"? What is the third letter there? Could there be other characters? Numbers? Dashes? So clearly things could be made complicated, if your teacher decides to make it so. But you need to do the work.
But, since this is clearly a homework assignment, I'm not going to do your work for you with not a bit of effort shown by you.

Cris LaPierre on 21 Sep 2021
There are different ascii codes for lowercase and uppercase letters (https://www.ascii-code.com/, see codes 65-122). I suspect the purpose of this assignment is to learn to use indexing to access and modify elements of a vector.
I think you would find the following chapters in MATLAB Onramp helpful.
• Ch 4 - Vectors and Matricies
• Ch 5 - Indexing into and Modifying Arrays
##### 2 CommentsShowHide 1 older comment
Cris LaPierre on 21 Sep 2021
Honestly, just forgot about the upper fucntion. There are many ways to solve this problem. This is just one.

Stephen on 21 Sep 2021
Edited: Stephen on 21 Sep 2021
T = 'my pet is hungry';
U = regexprep(T,'(?<=\<(\w{2})?)(\w)','\${upper(\$1)}')
U = 'My PeT Is HuNgry'
Of course because this is clearly homework you will not be able to hand in my answer as your own work.
Also note that teachers/tutors are perfectly capable of searching this forum: