Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Oracle Database, SQL, Application, Programming


Count your words


Reply
Views: 1431  
Thread Tools Rate Thread
  #1  
Old 08-11-2010, 02:38 PM
bholas bholas is offline
Award Winner
 
Join Date: Apr 2010
Posts: 4,977
Default Count your words

Looking for a simple function that would return the number of words, anything separated by spaces, in a specified string? Following function will do just that using pointers to strings. If you're new to string handling/parsing you might want to pay close attention to how the following function sets up a pointer to the original string and then travel through it, rather than using s[ 1 ], s[ 2 ], s[ 3 ], etc.
function WordsCount( s : string )
: integer;
var
ps : PChar;
nSpaces,
n : integer;
begin
n := 0;
s := s + #0;
ps := @s[ 1 ];
while( #0 <> ps^ ) do
begin
while((' ' = ps^)and(#0 <> ps^)) do
begin
inc( ps );
end;

nSpaces := 0;
while((' ' <> ps^)and(#0 <> ps^))do
begin
inc( nSpaces );
inc( ps );
end;
if ( nSpaces > 0 ) then
begin
inc( n );
end;
end;
Result := n;
end;

Reply With Quote
Reply

Tags
programming tips

New topics in Oracle Database, SQL, Application, Programming





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)