[comp.lang.pascal] Dose anybody have space det. code!!!!!!!!!

robby@nuchat.sccsi.com (Robert Oliver Jr.) (04/22/91)

dose any body have some code that can detect a space in a string[40] and replace it with a -, but if there is not a space then dont do anything ?
any help would be greatlee appreated.

Robert_Salesas@mindlink.bc.ca (Robert Salesas) (04/23/91)

I haven't run this but it should work...





Procedure ChangeSpace(Var S : String);
Var
  I : Word;
Begin
  I := 1;
  While (I < Length(S)) Do
    Begin        If (S[I] = ' ') Then
        S[I] := '-';
    End;
End; {ChangeSpace}


Or something along those lines anyways...

Rob
--
\--------------------------------------------------------------------/
\ Robert Salesas             + Usenet: Robert_Salesas@MINDLINK.bc.ca /
\ Eschalon Development Inc.  + CIS:    76625,1320    BYTE:  newdawn  /
\--------------------------------------------------------------------/

pschwart@vms.macc.wisc.edu (Paul Schwartz) (04/23/91)

In article <1991Apr22.060051.2238@nuchat.sccsi.com>, 
robby@nuchat.sccsi.com (Robert Oliver Jr.) writes...

>dose any body have some code that can detect a space in a string[40] and 
>replace it with a -, but if there is not a space then dont do anything ?
>any help would be greatlee appreated.

    In Turbo Pascal you can treat a string like an array, therefore

    for i := 1 to length(my_string) do   { for each char in the string }
    	if my_string[i] = chr(20) then   { if the char is a space }
    		my_string[i] = chr(45)   { replace it with a - }

    						- Z -

+-----------------------------------------------------------------------------+
|  PauL M SchwartZ              |   There are many causes worth dying for,    | 
|  PSCHWART@macc.wisc.edu       |         but none worth killing for.         |
|  PSCHWART@wiscmacc.BitNet     |                             - Gandhi        |
+-----------------------------------------------------------------------------+

granoff@vaxwrk.enet.dec.com (Mark H. Granoff) (04/23/91)

In article <1991Apr23.133227.10746@macc.wisc.edu>, pschwart@vms.macc.wisc.edu (Paul Schwartz) writes:
>    In Turbo Pascal you can treat a string like an array, therefore
>
>    for i := 1 to length(my_string) do   { for each char in the string }
>    	if my_string[i] = chr(20) then   { if the char is a space }
>    		my_string[i] = chr(45)   { replace it with a - }

Your example is fine, but your numeric base is wrong, once. :-)  

Replace chr(20) with either chr(32) or #32.  ASCII 32 is a space, not 20. 
However, 20 hex is 32 decimal, so I guess you are forgiven. :-)

---------------------------------------------------------------------------
Mark H. Granoff   |    Enterprise Integration Services/Engineering VAXworks
---------------------------------------------------------------------------
Digital Equipment Corporation | Internet: granoff@vaxwrk.enet.dec.com
129 Parker Street             | Usenet  : ...!decwrl!vaxwrk.enet!granoff
PKO2-1/M21                    | AT&T    : +1 508 493 4512
Maynard, MA 01754             | FAX     : +1 508 493 2240
---------------------------------------------------------------------------
Opinions herein are my own and do not necessarily reflect those of Digital.
---------------------------------------------------------------------------

sag@iplmail.orl.mmc.com (Steve Gabrilowitz) (04/24/91)

In article <1991Apr23.155720.1561@e2big.mko.dec.com>, granoff@vaxwrk.enet.dec.com (Mark H. Granoff) writes:
|> In article <1991Apr23.133227.10746@macc.wisc.edu>, pschwart@vms.macc.wisc.edu (Paul Schwartz) writes:
|> >    In Turbo Pascal you can treat a string like an array, therefore
|> >
|> >    for i := 1 to length(my_string) do   { for each char in the string }
|> >    	if my_string[i] = chr(20) then   { if the char is a space }
|> >    		my_string[i] = chr(45)   { replace it with a - }
|> 
|> Your example is fine, but your numeric base is wrong, once. :-)  
|> 
|> Replace chr(20) with either chr(32) or #32.  ASCII 32 is a space, not 20. 
|> However, 20 hex is 32 decimal, so I guess you are forgiven. :-)

A perfect example of why the code should have been written as:

  for i := 1 to length(my_string) do
    if my_string[i] = ' ' then
      my_string[i] := '_';

Easier to read, harder to make silly mistakes, and more portable (in  case you have some insane urge to port the program to an EBCDIC machine for example ;-)

-- 




                            Steve Gabrilowitz
                            Martin Marietta, Orlando Fl.
                            sag@iplmail.orl.mmc.com
                            Fidonet 1:363/1701

Harold.Ruby@f1999.n106.z1.fidonet.org (Harold Ruby) (04/24/91)

on 21 Apr 91  15:35:36 Robert Oliver Jr. said to All
 >dose any body have some code that can detect a space in a string[40] 
 >and replace it with a -, but if there is not a space then dont 
 >do anything ?
 >any help would be greatlee appreated.

procedure findspace (var thestring:string);
var loc:integer;
begin
  if length (thestring)=0 then exit;
  for loc:=1 to length(thestring) do if thestring[loc]=' ' then 
    thestring[loc]:='-';
end;

or {same header}

begin
  repeat
    if pos(' ',thestring)<>0 then thestring[pos(' ',thestring)]:='-';
  until pos(' ',thestring)=0;
end;

{note: the 2nd one requires no "loc" variable }

Harold

ricki@martha.informatik.rwth-aachen.de (Richard Breuer) (04/25/91)

robby@nuchat.sccsi.com (Robert Oliver Jr.) writes:

>dose any body have some code that can detect a space in a string[40] and replace it with a -, but if there is not a space then dont do anything ?
>any help would be greatlee appreated.

Try this (Turbo Pascal):

--- schanipp schanapp --------------------------------------------------------
procedure cb(var s: string40);

var
  p: integer;

begin
  { Calculate the position of the first space. 0, when there is none }
  p:=pos(' ',s);
  { Substitute it }
  if (p>0) then s[p] := '-'
end;
--- schanipp schanapp --------------------------------------------------------

I did not test it, but it should run.

Bye, Ricki.

vdra_ltd@uhura.cc.rochester.edu (Valerie Drake) (04/26/91)

In <ricki.672582014@rwthi3> ricki@martha.informatik.rwth-aachen.de (Richard Breuer) writes:

>robby@nuchat.sccsi.com (Robert Oliver Jr.) writes:

>>dose any body have some code that can detect a space in a string[40] and replace it with a -, but if there is not a space then dont do anything ?
>>any help would be greatlee appreated.

>Try this (Turbo Pascal):

>--- schanipp schanapp --------------------------------------------------------
>procedure cb(var s: string40);

>var
>  p: integer;

>begin
>  { Calculate the position of the first space. 0, when there is none }
>  p:=pos(' ',s);
>  { Substitute it }
>  if (p>0) then s[p] := '-'
>end;
>--- schanipp schanapp --------------------------------------------------------

>I did not test it, but it should run.

>Bye, Ricki.
This example only works if there is ONE space!  Here is an example to replace
all spaces with a "-":
==== Schnip ==================================================================
PROCEDURE replaceSpaces(var s:string);

CONST SPACE = #32;
      DASH  = #45;

VAR p : BYTE;

BEGIN
   p := pos(SPACE,s);
   WHILE (p > 0) DO
   BEGIN
      s[p] := DASH;
      p := pos(SPACE,s);
   END;
END;

(* or more generally *)

FUNCTION replaceAnyChar(s:STRING; target,replacement : char): STRING;
VAR P : BYTE;
    TString : String;
begin
   TString := s;
   p := pos(target,TString);
   WHILE (p > 0) DO
   BEGIN
      TString[p] := replacement;
      p := pos(target,TString);
   END;
   return(TString);
end;

====== Schnip ==============================================================

The second one will be more general and will replace any character with any
other, as well as leaving the original string intact if necessary.

Hope that helps even more :=>.

Lee
-- 
|                     |    +--+  +--+   o  | Carpe Diem, Seize the DAY!
|        //////       | |\/   .\/.   \/|  o| vdra_ltd@uhura.cc.rochester.edu
|       /_   _\       | |   )) /\ ((   | o | Val & Lee Drake (The Fish Faces)
+--,,,,----U----,,,,--+ |/\+--+  +--+/\| | | University of Rochester

defaria@hpcupt3.cup.hp.com (Andy DeFaria) (04/27/91)

>/ hpcupt3:comp.lang.pascal / sag@iplmail.orl.mmc.com (Steve Gabrilowitz) / 12:09 pm  Apr 23, 1991 /
>|> >    for i := 1 to length(my_string) do   { for each char in the string }
>|> >    	if my_string[i] = chr(20) then   { if the char is a space }
>|> >    		my_string[i] = chr(45)   { replace it with a - }
                                                                     ^
>A perfect example of why the code should have been written as:
>
>  for i := 1 to length(my_string) do
>    if my_string[i] = ' ' then
>      my_string[i] := '_';
                        ^
>
>Easier to read, harder to make silly  mistakes, and more portable (in case
>you have  some insane  urge to port the  program to an  EBCDIC  machine for
>example ;-)  

I agree but...

Of course you made the  mistake of using  underscore (_) where it  seems  a
hyphen was intended (-).

sdalelt@cc.curtin.edu.au (04/28/91)

In a msg dated [Mon Apr 22 1991], Robert Oliver Jr. wrote (to All):

 > ROJ: dose any body have some code that can detect a space in a
 > ROJ: string[40] and replace it with a -, but if there is not a
 > ROJ: space then dont do anything ?

You talk about string[40], so I presume you're using a version of pascal which
isn't ISO. (ie. turbo).

If you are using Turbo Pascal, here is an elegant solution.  Although I see
others have responded to you query, this is probably a better way to get the
end result:

  function change_spaces(s : string) : string;

  begin
    while pos(' ', s) > 0 do
      s[pos(' ', s)] := '-';
    change_spaces := s;
  end;

Cheers,
Lincoln.
_____________________________________________________________________________
 Internet: sdalelt@cc.curtin.edu.au                    Voice: +61-9-459-4701
           lincoln_dale@f627.n690.z3.fidonet.org
 FidoNet:  Lincoln Dale @ 3:690/627.0                   Data: +61-9-493-1534
 PSImail:  psi%050529452300070::sdalelt                       +61-9-493-2625
 UUCP:     uunet!munnari.oz!cc.curtin.edu.au!sdalelt
 Bitnet:   sdalelt%cc.curtin.edu.au@cunyvm.bitnet

popkin@cs.odu.edu (Brian N. Popkin) (04/28/91)

In article <1991Apr28.131624.7940@cc.curtin.edu.au> sdalelt@cc.curtin.edu.au writes:
>In a msg dated [Mon Apr 22 1991], Robert Oliver Jr. wrote (to All):
>
> > ROJ: dose any body have some code that can detect a space in a
> > ROJ: string[40] and replace it with a -, but if there is not a
> > ROJ: space then dont do anything ?
>
>You talk about string[40], so I presume you're using a version of pascal which
>isn't ISO. (ie. turbo).
>
>If you are using Turbo Pascal, here is an elegant solution.  Although I see
>others have responded to you query, this is probably a better way to get the
>end result:
>
>  function change_spaces(s : string) : string;
>
>  begin
>    while pos(' ', s) > 0 do
>      s[pos(' ', s)] := '-';
>    change_spaces := s;
>  end;
>

Theres so many ways to handle this simple problem...

Brian

*-----------------------------------------------------------------------------*
*                                                                             *
*  Brian Popkin	                                                              *
*  Computer Science Major - Systems Programing                                *
*  Old Dominion University                                                    *
*  Norfolk, Virginia USA                                                      *
*									      *
*  Others Areas Of Interest: Artificial Intelligence, Expert Systems,         *
*                            Networks, And Telecommunications                 *
*                                        				      *
*  Email Address: popkin@cs.odu.edu				              *
*                 popkin@xanth.cs.odu.edu                                     *
*                 popkin@gnu.ai.mit.edu                                       *
*									      *
*-----------------------------------------------------------------------------*

HCM100@psuvm.psu.edu (Hans C. Masing) (05/01/91)

Here's my $0.02 worth towards the space detection procedure:

(*--------------------------8< cut here-----------------------*)

PROCEDURE Change_Spaces_to_Dashes (VAR InStr : String[40]);

VAR
  I : Integer;  (* Indexing variable *)

BEGIN
  I := 1;
  While InStr[I] <> '' do
    Begin
      If InStr[I] = ' ' then
        InStr[I] := '-';
      I := I + 1;
    End;
END;

(*--------------------------8< cut here-----------------------*)

     This should be very generic and work with most version of
PASCAL, EBCIDIC or ASCII.  Now, who's going to write a recursive
version of this one?  :-)

     Hans
    /_/ _            /| /|  _      '         | HCM100 @ PSUVM.psu.edu
  _/ /_(_|_/|/_/)_  / |/ |_(_|_/)_/_/|/_(_)  | Hans C. Masing
                                      (__/