Board index » delphi » Capitalize String

Capitalize String

Hi,

I've only seldom used Pascal and that was in the old v5.5 days but I
badly need a routine or even preferrably a program that's already
compiled that will read a text file and then Capitalize the beginning of
every word and make the rest of each word lower case. This text file
contains a list of businesses that I'm going to be faxing a  proposal
to but each business and address is in all CAPS and it just doesn't look
very professional. Each line of text is one record and since I have
about 12,000 records I don't want to do this manually!

If anyone has a routine or even a program that will do this please email
me and let me know where I can find it. Any help would be appreciated.

Thanks ahead of time,

Kerry

 

Re:Capitalize String


Quote
Kerry Duvall wrote:

> Hi,

> I've only seldom used Pascal and that was in the old v5.5 days but I
> badly need a routine or even preferrably a program that's already
> compiled that will read a text file and then Capitalize the beginning of
> every word and make the rest of each word lower case. This text file
> contains a list of businesses that I'm going to be faxing a  proposal
> to but each business and address is in all CAPS and it just doesn't look
> very professional. Each line of text is one record and since I have
> about 12,000 records I don't want to do this manually!

> If anyone has a routine or even a program that will do this please email
> me and let me know where I can find it. Any help would be appreciated.

Here you go: I've used Pascal for years, but stuff like this
is easier and faster in Euphoria. You can download a free
copy of Euphoria from http://members.aol.com/fileseu

include wildcard.e

atom fn,fo
object line

fn = open("llama.doc","r") -- read mode (put your own
filename here)
fo = open("llama.out","w") -- write mode (ditto for output
filename)

function ULCase(object line)
  line = lower(line) -- convert to all lowercase
  for i = 1 to length(line) do
   if line[i] = ' ' and line[i+1] != ' ' then
   line[i+1] = upper(line[i+1])  -- upcase first char
   end if
  end for
 return line
end function

-- Main loop
 while 1 do
   line = gets(fn)
   if atom(line) then exit -- eof
   else puts(fo,ULCase(line)) -- write converted line to
output
   end if
 end while
 close(fn)
 close(fo)

Re:Capitalize String


Quote
> I've only seldom used Pascal and that was in the old v5.5 days but I
> badly need a routine or even preferrably a program that's already
> compiled that will read a text file and then Capitalize the beginning of
> every word and make the rest of each word lower case. This text file
> contains a list of businesses that I'm going to be faxing a  proposal
> to but each business and address is in all CAPS and it just doesn't look
> very professional. Each line of text is one record and since I have
> about 12,000 records I don't want to do this manually!

> If anyone has a routine or even a program that will do this please email
> me and let me know where I can find it. Any help would be appreciated.

This may help (although it's meant for names):

function Proper (S : string) : string;      { Make Name "proper" }
const FS1 = #31;
      FS2 = #33;
      FS3 = #34;
var I,J     : Word;
    LeaveIt : boolean;
    Sw      : string;
begin
  Sw := S; I := 1; J := Length(S); LeaveIt := true;
  if Pos (' MC',Sw) > 0 then Insert (FS1,Sw,Pos(' MC',Sw)+3);
  if Pos (' MAC',Sw) > 0 then Insert (FS1,Sw,Pos(' MAC',Sw)+4);
  if Pos ('III ',Sw) > 0 then Insert (FS2,Sw,Pos('III ',Sw))
  else
    if Pos ('II ',Sw) > 0 then Insert (FS3,Sw,Pos('II ',Sw));
  while I <= J do
    begin
      case Sw[I] of
        'A'..'Z' : begin
                     if LeaveIt then LeaveIt := false
                     else            Inc (Sw[I],32);
                     Inc (I)
                   end;
        '*'      : begin
                     Delete (Sw,I,1); Inc (I); Sw := Sw+' ';
                     LeaveIt := true
                   end;
        FS1      : begin
                     Delete (Sw,I,1); LeaveIt := true
                   end;
        FS2      : begin                                 { III }
                     Delete (Sw,I,1); LeaveIt := true; Inc (I,4)
                   end;
        FS3      : begin
                     Delete (Sw,I,1); LeaveIt := true; Inc (I,3) { II }
                   end;
        '-'      : begin
                     Inc (I); LeaveIt := true
                   end;
        ' '      : begin
                     Inc (I); LeaveIt := true
                   end;
        else       begin
                     Inc (I); LeaveIt := true
                   end
      end;  { case }
    end;
  if Copy(Sw,1,2) = 'Mc' then Sw[3] := UpCase(Sw[3]); Proper := Sw
end;  { Proper }

Re:Capitalize String


Well, here is how I would do it. Note that this is Pascal version 7.0. I
am not sure if it will work in 5.5 or not. Also, it seemed to work on
the file I used, but I cannot guarentee it will work with yours.

---------- [program starts here] ----------

Program Cap;

Const
  InF = 'in.txt';   {Change this to the file you want to read from.}
  OutF = 'out.txt'; {Change this to the file you want to write to.}

Var
  InFile, OutFile : Text;
  Line : String[255];
  Ct : Integer;

Begin
  Assign(InFile,InF);
  Reset(InFile);
  Assign(OutFile,OutF);
  ReWrite(OutFile);
  Repeat
    Readln(InFile,Line); {Reads entire line from the file.}
    {Change to uppercase if first letter is lowercase.}
    If Line[1] In ['a'..'z'] Then
      Line[1] := Chr(Ord(Line[1]) - 32);
    {Make every single letter lowercase for ease of programming.}
    For Ct := 2 To Length(Line) Do
      If Line[Ct] In ['A'..'Z'] Then
        Line[Ct] := Chr(Ord(Line[Ct]) + 32);
    {Note that this will make every character that has a space
     before it uppercase. In this program, that is how a word
     will be distinguished.}
    {If the character is a letter and the char before is a space,
     then make the character uppercase.}
    For Ct := 2 To Length(Line) Do
      If (Line[Ct] In ['a'..'z']) And (Line[Ct-1] = ' ') Then
        Line[Ct] := Chr(Ord(Line[Ct]) - 32);
    Writeln(OutFile,Line);  {Write newly formatted line to file.}
  Until (Eof(InFile));       {Stop when we have reached end of file.}
  Close(OutFile);
  Close(InFile);             {Close each file when done.}
  Writeln ('Done');
  Readln;
End. {End program.}

---------- [program ends here] ----------

--
Ken Robbins
puyrebel <AT> prodigy <DOT> net

"If everything seems to be going well, you have obviously overlooked
something."
-- Murphy's Eighth Law

Quote
Kerry Duvall wrote:

[snip]

Other Threads