Pascal

Modified: Wednesday, 22-12-2021 07:00 AM

Cho tệp văn bản có 2 dòng.

Dòng 1 chứa 1 xâu hoàn chỉnh.

Dòng 2 chứa 1 xâu con của xâu 1.

Đếm xem xâu 2 xuất hiện bao nhiêu lần trong xâu 1.



program quanli_string;
    var st,s:string;
    f:text; 
    i:byte;
procedure doc;
var ok:boolean;
begin
     assign(f,'bt.inp');
     {$i-}
     reset(f);
     ok:=ioresult=0;
     if not ok then
     begin
          writeln('khong ton tai file bt.inp');
          writeln('noi dung file bt.inp:');
          writeln('dong 1: string <chua qua xu ly>');
          writeln('dong 2: string con; dung de dem so lan xuat hien trong dong 1');
          readln;
          halt;
     end
     else
     begin
        reset(f);
        readln(f,st);
        readln(f,s);
        close(f);
     end;
end; 
procedure chuanhoa;
begin 
    while st[1]=#32 do
        delete(st,1,1);
    while st[length(st)]=#32 do
        delete(st,length(st),1);
    repeat        
        i:=pos('  ',st);
        delete(st,i,1);     
    until pos('  ',st)=0;
    for i:=1 to length(st) do
    begin
        if ((st[i]='.')or(st[i]=','))and(st[i+1]<>' ') then
        insert(' ',st,i+1);
    end;
    repeat                                    
        i:=pos(' .',st);
        delete(st,i,1);
        i:=pos(' ,',st);
        delete(st,i,1);      
    until (pos(' .',st)=0)and(pos(' ,',st)=0);
    if st[length(st)]<>'.' then
    st:=st+'.';
    st[1]:=upcase(st[1]);
    for i:=1 to length(st) do
        if st[i]='.' then
        st[i+2]:=upcase(st[i+2]);  
    assign(f,'bt.out');
    rewrite(f);
    writeln(f,st);                 
end;
procedure dem;               
var d:byte; ss,vt:string;  
begin        
    d:=0;             
    ss:='';
    for i:=1 to length(st) do
    if s=copy(st,i,length(s))then
    begin
         inc(d);
         str(i,vt);
         ss:=ss+vt+' ';
    end;     
    writeln(f,'[',s,'] xuat hien ',d,' lan trong [',st,']');
    writeln('o cac vi tri: ',ss);
    close(f);
end;
begin
    doc;
    chuanhoa;
    dem;   
    writeln('da xu ly xong');
    readln;
end.