Recent

Author Topic: SOLVED Capitalize word after each space not working  (Read 484 times)

DreamVB

  • New Member
  • *
  • Posts: 24
SOLVED Capitalize word after each space not working
« on: July 04, 2022, 12:41:20 am »
Hi, I been making a little text editor and I am adding in some features to deal with converting a selection of text, I got the uppercase,lowercase and toggle-case working however I am stuck on the capitalize case, what I done so far is made it so you can select a string with a space after each word and it will uppercase the first letter after the space.

I want to add support for TABS also the code below works with tabs but if you insert a space before or after the tab is does not uppercase the next none space char can someone please have a look what I have done wrong thanks.

Code: Pascal  [Select][+][-]
  1. function Capitalize(const S : String) : String;
  2. var
  3.   flag : Boolean;
  4.   S1 : String;
  5.   CH : String;
  6.   X : Integer;
  7. begin
  8.  
  9.   flag := True;
  10.   S1 := '';
  11.  
  12.    For X := 1 to Length(S) do begin
  13.        CH := Copy(S,X,1);
  14.  
  15.        if flag then
  16.        begin
  17.          if (CH <> ' ') or (CH <> #9) then begin
  18.             S1 := S1 + UpperCase(CH);
  19.             flag := False;
  20.          end
  21.          else
  22.          begin
  23.              S1 := S1 + CH;
  24.          end;
  25.        end
  26.        else
  27.        begin
  28.            S1 := S1 + Lowercase(CH);
  29.            flag := (CH = ' ') OR (ch = #9);
  30.        end;
  31.    end;
  32.  
  33.    Result := S1;
  34. end;
« Last Edit: July 04, 2022, 08:06:29 pm by DreamVB »
When your dreams come true

y.ivanov

  • Hero Member
  • *****
  • Posts: 521
Re: Capitalize word after each space not working
« Reply #1 on: July 04, 2022, 01:03:19 am »
Hi, I been making a little text editor and I am adding in some features to deal with converting a selection of text, I got the uppercase,lowercase and toggle-case working however I am stuck on the capitalize case, what I done so far is made it so you can select a string with a space after each word and it will uppercase the first letter after the space.

I want to add support for TABS also the code below works with tabs but if you insert a space before or after the tab is does not uppercase the next none space char can someone please have a look what I have done wrong thanks.

Code: Pascal  [Select][+][-]
  1. function Capitalize(const S : String) : String;
  2. var
  3.   flag : Boolean;
  4.   S1 : String;
  5.   CH : String;
  6.   X : Integer;
  7. begin
  8.  
  9.   flag := True;
  10.   S1 := '';
  11.  
  12.    For X := 1 to Length(S) do begin
  13.        CH := Copy(S,X,1);
  14.  
  15.        if flag then
  16.        begin
  17.          if (CH <> ' ') or (CH <> #9) then begin
  18.             S1 := S1 + UpperCase(CH);
  19.             flag := False;
  20.          end
  21.          else
  22.          begin
  23.              S1 := S1 + CH;
  24.          end;
  25.        end
  26.        else
  27.        begin
  28.            S1 := S1 + Lowercase(CH);
  29.            flag := (CH = ' ') OR (ch = #9);
  30.        end;
  31.    end;
  32.  
  33.    Result := S1;
  34. end;

At line 17: the opposite of (CH = ' ') OR (ch = #9) is (CH <> ' ') and (CH <> #9)
i.e. not(A or B) = (not A) and (not B)

It is called "DeMorgan Law":
https://en.wikipedia.org/wiki/De_Morgan%27s_laws
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

dje

  • Full Member
  • ***
  • Posts: 111
Re: Capitalize word after each space not working
« Reply #2 on: July 04, 2022, 02:19:34 am »

jamie

  • Hero Member
  • *****
  • Posts: 4765
Re: Capitalize word after each space not working
« Reply #3 on: July 04, 2022, 03:51:41 am »
Hmm.
Code: Pascal  [Select][+][-]
  1. Function CamelCaseWords(S:String):String;
  2. Var
  3.   LookingForFirstChar:Boolean = True;
  4.   I:Integer;
  5. begin
  6.   Result := S;
  7.   For I := 1 to Length(Result) do
  8.     Begin
  9.       If (LookingForFirstChar) And (Result[I]in['A'..'z'])
  10.        Then Result[I] :=UpCase(Result[I]);
  11.       LookingForFirstChar := Result[I] in [' ',#8];
  12.     end;
  13. end;                  
  14.  
Study that one. That should avoid UTF8 mixup and only cap Ansi.

 But be aware, there does not expand the TAB.

The only true wisdom is knowing you know nothing

y.ivanov

  • Hero Member
  • *****
  • Posts: 521
Re: Capitalize word after each space not working
« Reply #4 on: July 04, 2022, 10:58:49 am »
@jamie
The name of the function is misleading. It is not doing a camelCase.
The expression (Result[ I ]in['A'..'z']) includes non-letters, which is not fatal, but not very educational.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

jamie

  • Hero Member
  • *****
  • Posts: 4765
Re: Capitalize word after each space not working
« Reply #5 on: July 04, 2022, 12:14:09 pm »
And you don't do such things yourself ?


The only true wisdom is knowing you know nothing

DreamVB

  • New Member
  • *
  • Posts: 24
Re: Capitalize word after each space not working
« Reply #6 on: July 04, 2022, 08:06:10 pm »
Thanks y.ivanov it's working now, thanks to the rest that also give answers.
When your dreams come true

 

TinyPortal © 2005-2018