Recent

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

DreamVB

  • Full Member
  • ***
  • Posts: 100
    • Memo Pad
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 »
Dream Believe Achieve

alpine

  • Hero Member
  • *****
  • Posts: 1032
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: 134
Re: Capitalize word after each space not working
« Reply #2 on: July 04, 2022, 02:19:34 am »

jamie

  • Hero Member
  • *****
  • Posts: 6077
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

alpine

  • Hero Member
  • *****
  • Posts: 1032
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: 6077
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

  • Full Member
  • ***
  • Posts: 100
    • Memo Pad
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.
Dream Believe Achieve

 

TinyPortal © 2005-2018