Recent

Author Topic: String manipulation problem  (Read 3445 times)

whitehat

  • Jr. Member
  • **
  • Posts: 93
String manipulation problem
« on: October 07, 2019, 09:15:44 pm »
i wanna do like this guys but everytime i got a problem like my table is empty
typeHtml = anna,mat,alex
tType[1]= anna
tType[2]= mat
tType[3]= alex

Code: Pascal  [Select][+][-]
  1. pnl:=1;
  2. for i:=1 to length(typeHtml) do
  3.   begin
  4.   if typeHtml[i]=',' then
  5.  
  6.  
  7.   tType[pnl]:=copy(typeHtml,1,pos(',',typeHtml)-1);
  8.   pnl:=pnl+1;
  9.  
  10.    delete(typeHtml,1,pos(',',typeHtml));
  11.   end;

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: String manipulation problem
« Reply #1 on: October 07, 2019, 09:33:43 pm »
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses
  3.   sysutils, classes;
  4.  
  5. var
  6.   typeHtml: string = 'anna,mat,alex';
  7.   tType: TStringArray;
  8. begin
  9.   tType := typeHtml.Split(',');
  10. end.

Bart

whitehat

  • Jr. Member
  • **
  • Posts: 93
Re: String manipulation problem
« Reply #2 on: October 07, 2019, 09:37:23 pm »
bart the problem that i have, i need just to use crt in lazarus cause they want to teach us algorithm only with the crt  library

whitehat

  • Jr. Member
  • **
  • Posts: 93
Re: String manipulation problem
« Reply #3 on: October 07, 2019, 09:38:47 pm »
and the tType is a table like
Code: Pascal  [Select][+][-]
  1. type
  2. tab= array[1..20] of string;
  3. var
  4. tType:tab;

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: String manipulation problem
« Reply #4 on: October 07, 2019, 09:51:15 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.   typeHtml: string = 'anna,mat,alex';
  3.   tType: array[1..20] of string;
  4.   i,p: Integer;
  5. begin
  6.   i := 1;
  7.   while typeHtml <> '' do
  8.   begin
  9.     p := Pos(',', typeHtml);
  10.     if p = 0 then
  11.     begin
  12.       tType[i] := typeHtml;
  13.       Break;
  14.     end;
  15.     tType[i] := copy(typeHtml,1,p-1);
  16.     Delete(typeHtml, 1, p);
  17.     Inc(i);
  18.   end;
  19.   for i := 1 to 20 do writeln('"',tType[i],'"');
  20. end.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: String manipulation problem
« Reply #5 on: October 07, 2019, 10:02:04 pm »
Alternatively (but I prefer my first one):

Code: Pascal  [Select][+][-]
  1. var
  2.   typeHtml: string = 'anna,mat,alex';
  3.   tType: array[1..20] of string;
  4.   i,p: Integer;
  5.   s: String;
  6.   c: char;
  7. begin
  8.   i := 1;
  9.   s := '';
  10.   for p := 1 to Length(typeHtml) do
  11.   begin
  12.     c := typeHtml[p];
  13.     if (c = ',') then
  14.     begin
  15.       tType[i] := s;
  16.       s := '';
  17.       inc(i);
  18.     end
  19.     else
  20.     begin
  21.       s := s + c;
  22.     end;
  23.   end;
  24.   if (s <> '') then tType[i] := s;
  25.   for i := 1 to 20 do writeln('"',tType[i],'"');
  26. end.  

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: String manipulation problem
« Reply #6 on: October 07, 2019, 10:13:11 pm »
Or (if you're not allowed to use Pos function):

Code: Pascal  [Select][+][-]
  1. var
  2.   typeHtml: string = 'anna,mat,alex';
  3.   tType: array[1..20] of string;
  4.   i, p: Integer;
  5. begin
  6.   i := 1;
  7.   p := 1;
  8.   while typeHtml <> '' do
  9.   begin
  10.     if (typeHtml[i] = ',') then
  11.     begin
  12.       tType[p] := Copy(typeHtml, 1, i-1);
  13.       Delete(typeHtml, 1, i);
  14.       i := 0;
  15.       Inc(p);
  16.     end
  17.     else if i = length(typeHtml) then
  18.     begin
  19.       tType[p] := typeHtml;
  20.       typeHtml := ''
  21.     end;
  22.     Inc(i);
  23.   end;
  24.   for i := 1 to 20 do writeln('"',tType[i],'"');
  25. end.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: String manipulation problem
« Reply #7 on: October 07, 2019, 10:20:48 pm »
Hi!

Due to the fact that teachers love short code I cut down Bart's example 1 two or three lines:

Code: Pascal  [Select][+][-]
  1.     var
  2.       typeHtml: string = 'anna,mat,alex';
  3.       tType: array[1..20] of string;
  4.       i: integer= 1;    p: Integer;
  5.     begin
  6.         repeat      
  7.         p := Pos(',', typeHtml);
  8.         if p = 0 then
  9.           tType[i] := typeHtml
  10.            else  begin
  11.                    tType[i] := copy(typeHtml,1,p-1);
  12.                    Delete(typeHtml, 1, p);
  13.                    Inc(i);
  14.                   end;
  15.       until p = 0;
  16.       for i := 1 to 20 do writeln('"',tType[i],'"');
  17.     end.
  18.  


Happy lesson
Winni

whitehat

  • Jr. Member
  • **
  • Posts: 93
Re: String manipulation problem
« Reply #8 on: October 07, 2019, 10:38:24 pm »
Take this for exemple and you will see what happen:
<for it='5' type='password,button,text'/>
the output is : text'/> it's the same problem
Code: Pascal  [Select][+][-]
  1. program htmlnew2;
  2. uses crt;
  3. type
  4.   tab= array [1..50]of string;
  5. var
  6.   t,tType:tab;
  7.   n:integer;
  8. procedure remplire (var t:tab;var n:integer);
  9. var
  10.   i:integer;
  11. begin
  12.   i:=1;
  13.   n:=1;
  14.   repeat
  15.    readln(t[i]);
  16.     i:=i+1;
  17.     n:=n+1;
  18.   until ReadKey=#42;
  19. end;
  20.  
  21. procedure forlop (var t,tType:tab;n:integer);
  22. var
  23.   p,pnl,solking,i:integer;
  24.   iteration,typeHtml:string;
  25. begin
  26.   iteration:='';
  27.   for i:=1 to n do
  28.   begin
  29.   if pos('for',t[i])<>0 then
  30.   iteration:=copy(t[i],10,1);
  31.  
  32.  
  33.   end;
  34.  
  35.  
  36.  
  37.   typeHtml:='';
  38.    //19
  39.  
  40.    for i:=1 to n do
  41.   begin
  42.   if pos('for',t[i])<>0 then
  43.   typeHtml:=copy(t[i],19,(length(t[i]))-2);
  44.   end;
  45.    solking:=1;
  46.     for i:=1 to length(typeHtml) do
  47.    begin
  48.    if typeHtml[i]=',' then
  49.    solking:=solking+1;
  50.  
  51.    end;
  52.     pnl:=1;
  53.   {
  54.   for i:=1 to length(typeHtml) do
  55.   begin
  56.   if typeHtml[i]=',' then
  57.  
  58.   writeln('hello poto');
  59.   tType[pnl]:=copy(typeHtml,1,pos(',',typeHtml)-1);
  60.   pnl:=pnl+1;
  61.  
  62.    delete(typeHtml,1,pos(',',typeHtml));
  63.   end;
  64.   }
  65.        i:=1;
  66.        p:=0;
  67.     repeat
  68.         p := Pos(',', typeHtml);
  69.         if p = 0 then
  70.           tType[i] := typeHtml
  71.            else  begin
  72.                    tType[i] := copy(typeHtml,1,p-1);
  73.                    Delete(typeHtml, 1, p);
  74.                    Inc(i);
  75.                   end;
  76.       until p = 0;
  77.  
  78.  
  79.   repeat
  80.    writeln(iteration);
  81.    writeln(typeHtml);
  82.    writeln(solking);
  83.    for i:=1 to solking do
  84.    begin
  85.    writeln(tType[solking]);
  86.    end;
  87.    until ReadKey=#42;
  88.    end;
  89. begin
  90.  remplire(t,n);
  91.  forlop(t,tType,n);
  92. end.

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: String manipulation problem
« Reply #9 on: October 07, 2019, 10:49:00 pm »
Take this for exemple and you will see what happen:

What in that example is the text (typeHtml)?
If it is 'password,button,text' then all of my code examples split it correctly.

Here's an even shorter version (but seriously more stupid):

Code: [Select]
var
  typeHtml: string = 'password,button,text';
  tType: array[1..20] of string;
  i,p: Integer;
begin
  p := 1;
  for i := 1 to length(typeHtml) do
  begin
    if typeHtml[i] = ',' then
      Inc(p)
    else
      tType[p] := tType[p] + typeHtml[i];
  end;
  for i := 1 to 20 do writeln('"',tType[i],'"');
end.

It outputs:
Code: [Select]
"password"
"button"
"text"
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
[code]

Bart

whitehat

  • Jr. Member
  • **
  • Posts: 93
Re: String manipulation problem
« Reply #10 on: October 07, 2019, 10:51:34 pm »
this is the output i get barn :
for i='5' type='text,button,password'>

xt,button,password'>
3
password'>
password'>
password'>

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: String manipulation problem
« Reply #11 on: October 07, 2019, 11:12:28 pm »
You have made us belive that 'password,button,text' is the input which we have to split.
Now you say it is <for it='5' type='password,button,text'/>.
That is a whole different ballgame.

Try to split (no pun intended) the problem.
Splitting 'password,button,text' is the easy part:

Code: Pascal  [Select][+][-]
  1. type
  2.   Tab= array[1..50] of string;
  3.  
  4. procedure Split(S: String; var T: Tab; var Count: Integer); //or : ; out Count); if that syntax is allowed for you
  5. {
  6. Preconditions:
  7.   Comma (',') is used as separator
  8.   S is not allowed to end in a comma
  9. Postconditions:
  10.   Count is set to the number of strings found
  11.   T[1]..T[Count] hold the respective strings
  12.   T[Count+1]..T[50] are empty strings
  13. }
  14.  
  15. var
  16.   p: Integer;
  17. begin
  18.   FillChar(T, SizeOf(T), #0);  //clear all entries
  19.   Count := 1;
  20.   while S <> '' do
  21.   begin
  22.     p := Pos(',', S);
  23.     if p = 0 then
  24.     begin
  25.       T[Count] := S;
  26.       Break;
  27.     end;
  28.     T[Count] := copy(S,1,p-1);
  29.     Delete(S, 1, p);
  30.     Inc(Count);
  31.   end;
  32. end;
  33.  
  34. var
  35.   typeHtml: string = 'anna,mat,alex,password,button,text';
  36.   tType: tab;
  37.   i, Count: Integer;
  38. begin
  39.   Split(typeHtml, tType, Count);
  40.   writeln('Count = ',Count);
  41.   for i := 1 to Count do writeln('"',tType[i],'"');
  42. end.

Output:
Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
Count = 6
"anna"
"mat"
"alex"
"password"
"button"
"text"

Now write something to extract 'password,button,text' form the whole inputstring.
In your example this is "<for it='5' type='password,button,text'/>"
Is that somehow fixed, or can it be anything like e.g.:
<foo to='bar' class='type red' id='blue' onClick='return foobar();' type   =   'password,button,tex' hidden='true'   /     >
(Notice the word 'type' in the class atrribute...)

Bart
« Last Edit: October 07, 2019, 11:41:39 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: String manipulation problem
« Reply #12 on: October 07, 2019, 11:40:36 pm »
Code: Pascal  [Select][+][-]
  1. function ExtractTypeHtmlFromTagSimple(Tag: String): String;
  2. {
  3. Precondition:
  4.   Tag is in the form of "<for SomeId='SomeValue' type='comma,separated,values'/>"
  5.   SomeId cannot be the string 'type'
  6.   None of the comma-separated values itself can contain a single quote as part of the value
  7.   There will be no space before or after the equals sign after "type"
  8. PostCondition:
  9.   The result is set to 'comma,separated,values' or an empty string if type='comma,separated,values' is not found
  10. }
  11. var
  12.   p: Integer;
  13. begin
  14.   Result := '';
  15.   if Length(Tag) < Length('<type=''''/>') then Exit;
  16.   if (Tag[1] <> '<') and (Tag[Length(Tag)] <> '>') and (Tag[Length(Tag)-1] <> '/') then Exit;
  17.   p := Pos('type=''', Tag);  //also find the first single quote atfer the equals sign
  18.   if p = 0 then Exit;
  19.   Inc(p, Length('type='''));
  20.   Result := Copy(Tag, p, MaxInt);
  21.   p := Pos('''', Result);
  22.   if p > 0 then
  23.     Delete(Result, p, MaxInt)
  24.   else
  25.     Result := '';
  26. end;

And now the program:

Code: Pascal  [Select][+][-]
  1. var
  2.   typeHtml: string = 'anna,mat,alex,password,button,text';
  3.   Tag: string = '<for it=''bar'' type=''password,button,text'' hidden=''true''/>';
  4.   tType: tab;
  5.   i, Count: Integer;
  6. begin
  7.   typeHtml :=  ExtractTypeHtmlFromTagSimple(Tag);
  8.   writeln('typeHtml = "',typeHtml,'"');
  9.   Split(typeHtml, tType, Count);
  10.   writeln('Count = ',Count);
  11.   for i := 1 to Count do writeln('"',tType[i],'"');
  12. end.

Output:
Code: [Select]
typeHtml = "password,button,text"
Count = 3
"password"
"button"
"text"

Parsing arbitrary tags like "<foo to='bar' class='type red' id='blue' onClick='return foobar();' type   =   'password,button,tex' hidden='true'   /     >" is almost impossible without the use of other units/functions than are available in the system unit.


Going to sleep now ...


Bart

whitehat

  • Jr. Member
  • **
  • Posts: 93
Re: String manipulation problem
« Reply #13 on: October 08, 2019, 10:28:15 pm »
so this is my what i done dude and i get error

Code: Pascal  [Select][+][-]
  1. program final;
  2. uses crt;
  3. type
  4.   tab= array[1..20] of string;
  5.   var
  6.     t,tTag:tab;
  7.     n:integer;
  8.     numberFor:string;
  9.     tag:string;
  10. procedure remplire (var t:tab;var n:integer);
  11. var
  12.   i:integer;
  13. begin
  14. i:=1;
  15. n:=1;
  16. repeat
  17.   readln(t[i]);
  18.    i:=i+1;
  19.    n:=n+1;
  20.  until ReadKey=#42;
  21. end;
  22. // this procedure used to get  the 5 number after it='  <for it='5' type='text,password'>
  23. procedure getNumber (t:tab;n:integer;var numberFor:string;var tag:string);
  24. var
  25.   i:integer;
  26. begin
  27. for i:=1 to n do
  28. begin
  29. if pos('for',t[i])<>0 then
  30. tag:=t[i];
  31. numberFor:=copy(t[i],10,1);
  32. end;
  33. repeat
  34. writeln(numberFor);
  35. until ReadKey=#42;
  36.  
  37. end;
  38. //this function is for cleaning tag like if i find <for it='5' type='text,password'>  / the tTag (table) value is  :text,password
  39. procedure cleanTag (tag:string;var tTag:tab);
  40. var
  41. j,i,NumberVergul:integer;
  42. begin
  43.  
  44. NumberVergul:=1;
  45. for i:=1 to length(tag) do
  46. begin
  47. if tag[i] <> ',' then
  48. NumberVergul:=NumberVergul+1;
  49. end;
  50.  
  51. for j:=1 to NumberVergul do
  52. begin
  53. for i:=1 to length(tag) do
  54. if tag[i] <> ',' then
  55. tTag[j]:=tag[i]
  56. else
  57.   tTag[j]:=tag[i+1];
  58.  
  59.  
  60. end;
  61.  
  62.  
  63. repeat
  64.   for i:=1 to NumberVergul do
  65.   begin
  66.   writeln(tTag[i]);
  67.   end;
  68.  until ReadKey=#42;
  69.  
  70.   end;
  71.  
  72. begin
  73. remplire (t,n);
  74. getNumber(t,n,numberFor,tag);
  75. cleanTag (tag,tTag);
  76. end.

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: String manipulation problem
« Reply #14 on: October 08, 2019, 11:36:55 pm »
What error?

Bart

 

TinyPortal © 2005-2018