DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Pascal and Delphi (http://www.daniweb.com/forums/forum124.html)
-   -   pascal word and letter counting (http://www.daniweb.com/forums/thread58609.html)

tom3005 Oct 19th, 2006 2:16 pm
pascal word and letter counting
 
hey

I am learning pascal and need to write a program where, when the user enters a sentence finishing with a full stop it prints the number of words in the sentance and the number of words with over 3 letters in.

I have no idea how to count the words in a sentance or the letters in a word. I also dont no how to regonise the full stop as the end of the sentence. So as you can see im quite stuck!

Any help getting started would be much appreciated

tom

Micheus Oct 26th, 2006 4:20 am
Re: pascal word and letter counting
 
Quote:

Originally Posted by tom3005 (Post 265262)
hey

I am learning pascal and need to write a program where, when the user enters a sentence finishing with a full stop it prints the number of words in the sentance and the number of words with over 3 letters in.

I have no idea how to count the words in a sentance or the letters in a word. I also dont no how to regonise the full stop as the end of the sentence. So as you can see im quite stuck!

Any help getting started would be much appreciated

tom

You can count words in a sentance by looking for space character (or carrie return if present - Does is possible?) until the end of sentance (full stop ???). So, extracting word by word from sentance you can count how many letters it has in too.

WhiteAvenger Oct 30th, 2006 5:53 pm
Re: pascal word and letter counting
 
If you need help getting started, These are Code Blocks I'd use:

To get the length of a string:
length(<somestring>)      {This gives you - have a guess - the Length of a string.}

To get only the string until the full stop:
var ct:Integer; somestring, croppedString:String; Flag:Boolean;
...
Begin
...
Flag := False;
ct:= 0;
While not Flag Do
  Begin
    Inc(ct);
    Flag := (somestring[ct] = '.') or (ct = length(somestring));        {somestring[2] gives you the 2nd letter of the string.}
  End;
croppedString:= copy(somestring, 1,ct);        {copy(String, Index, Count) gives you the "count" letters after "Index".}
...
End.

To count the number of letters:
var ct,NumOfLetters: Integer; somestring: String;
...
Begin
...
NumOfLetters := 0;
For ct:= 1 to length(somestring) Do
      If (somestring[ct] <> ' ') Then                Inc(NumOfLetters);                  {Increase NumOfLetters.}
...
End.

I'll have to think about how to count the number of words with over three letters, though - I'll try to post it tomorrow.

Greetz

WhiteAvenger


All times are GMT -4. The time now is 2:55 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC