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 次查看(过去 30 天)
.
  3 个评论

请先登录,再进行评论。

回答(3 个)

John BG
John BG 2016-12-22
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 个评论
David Barry
David Barry 2016-12-22
  • 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.

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-12-22
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 2016-12-22

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by