Filter löschen
Filter löschen

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 Ansichten (letzte 30 Tage)
.
  3 Kommentare
David Barry
David Barry am 21 Dez. 2016
Try this.
out = count('here is my question', 'here is the effort I put in to answering it')

Melden Sie sich an, um zu kommentieren.

Antworten (3)

John BG
John BG am 22 Dez. 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 Kommentar
David Barry
David Barry am 22 Dez. 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.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 22 Dez. 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 am 22 Dez. 2016

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by