User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 375,276 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,065 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser:
Dec 17th, 2004
Views: 6,709
A function that takes a string and returns the number of words in the string. An example of using pointers in Pascal.
pascal Syntax | 1 stars
  1. // count words in a string using pointers
  2. // Pascal code used in a Delphi 3.0 program
  3.  
  4. function WordsCount(s: string ): integer;
  5. var
  6. ps: PChar;
  7. nSpaces, n: integer;
  8. begin
  9. n := 0;
  10. // make it a string that ends with value zero
  11. s := s + #0;
  12. // point to start of string s
  13. ps := @s[ 1 ];
  14. // keep loop going up to zero ending
  15. while( #0 <> ps^ ) do
  16. begin
  17. // checks for space = ' '
  18. while((' ' = ps^) and (#0 <> ps^)) do
  19. begin
  20. inc( ps );
  21. end;
  22. nSpaces := 0;
  23. while((' ' <> ps^) and (#0 <> ps^)) do
  24. begin
  25. inc(nSpaces);
  26. inc(ps);
  27. end;
  28. if (nSpaces > 0) then
  29. begin
  30. inc( n );
  31. end;
  32. end;
  33. Result := n;
  34. end;
  35.  
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 6:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC