Clear Filters
Clear Filters

can anyone help me... Write a MATLAB script (called count.m) which reads a string from standard input and then counts the number of words in that string

2 views (last 30 days)
.
  3 Comments

Sign in to comment.

Answers (3)

John BG
John BG on 22 Dec 2016
assume only spaces, comas, full stop, .. are used between words but not before the very first word and the very last word, then the amount of words is the amount of separators +1.
The most simple version of the script you want would be:
str1='What have you done so far? Please post your code and ask specific questions about it. Then we can offer suggestions and corrections'
amount_words=numel(strfind(str1,' '))+1
amount_words =
23
hey it works :)
you can then start refining finding ':' ';' '?' that may be isolated and might be counted as words yet they shouldn't.
if you find my answer useful would you please mark it as Accepted Answer by clicking on the ACCEPT ANSWER button?
thanks in advance for time and attention
John BG
Note: feel free to ignore all sorts of comments telling that you have to do your homework, and telling you how to ask questions.
  1 Comment
David Barry
David Barry on 22 Dec 2016
  • 1) This fails if there is more than once space between words.
  • 2) Do not tell him to ignore our advice. He needs to learn how to use the forum correctly so that he can benefit in the long term and actually learn MATLAB.

Sign in to comment.


Image Analyst
Image Analyst on 22 Dec 2016
Hint:
str = 'i need help with my homework'
words = strsplit(str, ' ')
numWords = length(.......
Use inputdlg() to get the string from the user. See its example in its help.

Walter Roberson
Walter Roberson on 22 Dec 2016

Categories

Find more on Programming 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!