How can I count, sum, and display the number of special characters in a sentence? (Using for loops/if statements/strcmp/strfind/strread)
3 views (last 30 days)
Show older comments
I counted the number of characters per sentence, but now I have to count how many special characters are in each sentence (seperately) and display the number of special characters for each sentence individually. The beginning of my code is:
s1='Hi my name is.'
s2='I am 19 years old.'
s3='I live in .'
s4='I would like to get a 100% in this class.'
s5='My major is Chemical Engineering.'
s=[s1 ' ' s2 ' ' s3 ' ' s4 ' ' s5]
sent1=length(s1);
sent2=length(s2);
sent3=length(s3);
sent4=length(s4);
sent5=length(s5);
fprintf('The total number of characters in sentence 1 is %d.\n',sent1)
fprintf('The total number of characters in sentence 2 is %d.\n',sent2)
fprintf('The total number of characters in sentence 3 is %d.\n',sent3)
fprintf('The total number of characters in sentence 4 is %d.\n',sent4)
fprintf('The total number of characters in sentence 5 is %d.\n',sent5)
I know this might be easy but I'm very new with Matlab and programming in general.
Also, how would I go about doing this for the number of vowels in each sentence, the number of spaces in each sentence, the number of "the"'s in each sentence, the number of "and"'s in each sentence, and find and display the most frequent word in ALL sentences combined?
0 Comments
Answers (1)
Walter Roberson
on 9 Nov 2013
Please see the advice we recently gave to someone else with the same name as yours; see http://www.mathworks.co.uk/matlabcentral/answers/105425-how-to-ascertain-number-of-special-characters-in-a-string
0 Comments
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!