例:字符串函数调用示例
This presentation is the property of its rightful owner.
Sponsored Links
1 / 18

任意输入一个句子(以‘ .’ 号结束,长度在 255 以内)和一个单词(长度小于等于 8 ),统计该单词在句子中出现的次数 PowerPoint PPT Presentation


  • 140 Views
  • Uploaded on
  • Presentation posted in: General

例:字符串函数调用示例 program samplefun; const tur='turbo'; pas='pascal'; var  st:string[60]; p:byte; begin st:=concat(tur,pas,'is better than','stand',pas,'.'); writeln(st); writeln(length(st)); st:=copy(st,29,15); writeln(st); p:=pos(pas,st); writeln(p); p:=pos(tur,st); writeln(p); end.

Download Presentation

任意输入一个句子(以‘ .’ 号结束,长度在 255 以内)和一个单词(长度小于等于 8 ),统计该单词在句子中出现的次数

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


255 8

例:字符串函数调用示例program samplefun;consttur='turbo';pas='pascal';var st:string[60];p:byte;beginst:=concat(tur,pas,'is better than','stand',pas,'.');writeln(st);writeln(length(st));st:=copy(st,29,15);writeln(st);p:=pos(pas,st);writeln(p);p:=pos(tur,st);writeln(p);end.


255 8

  • 任意输入一个句子(以‘.’号结束,长度在255以内)和一个单词(长度小于等于8),统计该单词在句子中出现的次数

  • 分析:用字符串变量s存放读如的句子,S1存放要查找的单词,另用变量K统计单词出现的次数。开始时置K为0,并调用pos函数找出第一个单词在句子中的位置,若返回值不为0,则再调用delete过程将这个单词在句子中删除,k加1,然后再调用pos函数……,若返回值为0,则表示句子中已无这个单词,循环结束。


255 8

  • Var s,s1:string; t,k:integer;

  • Begin

  • Writeln(‘please input s’); readln(s);

  • Writeln(‘please input s1’);readln(s1);

  • k:=0;

  • Repeat

  • t:=pos(s1,s);

  • If t<>0 then

  • Begin

  • Delete(s,t,length(s1));

  • k:=k+1;

  • End;

  • Until t=0;

  • Writeln(k);

  • End.


255 8

  • 输入一个英语短句,以‘.’结束,求出其中最长单词的长度


255 8

Var s:string; ch:string[1]; I,max,l:integer;

Begin

max:=0; l:=0;

readln(s);

for i:=0 to length(s) do

begin

ch:=copy(s,I,1);

if (ch<>’ ‘)and(ch<>’.’)then l:=l+1

else if l>max then begin max:=l; l:=0 end

else l:=0;

end;

Writeln(max);

End.


255 8

统计输入的n个英语单词中以“con”开头的单词个数以及字母“e”出现的频率。


255 8

Var wd:string[30];

i,j,l,n,count,e,sum:integer;

Begin

readln(n);count:=0;e:=0;sum:=0;

for i:=1 to n do

begin

readln(wd)

l:=length(wd);

if copy(wd,1,3)=‘con’ then count:=count+1

for j:=1 to l do

if wd[j]=‘e’ then e:=e+1;

sum:=sum+l;

end;

writeln(count);

writeln(e/sum*100:5:2,’%’);

readln;

End.


255 8

例:字符串过程调用示例program guocheng;consttypedstring:string= 'turbo p

ascal is better than standard pascal.';total:real=388.4;vartotalstring:string[60];integervalue:integer;realvalue:real;status:integer;

begindelete(typedstring,13,40);writeln(typedstring);insert('using',typedstring,1);writeln(typedstring);str(total:8:2,totalstring);writeln(totalstring);str(total,totalstring);writeln(totalstring);val('-33',integervalue,status);writeln(integervalue,'':2,status);val('-33.99',realvalue,status);writeln(realvalue:6:2,'':2,status);end. 


255 8

  • 例3、对给定的10个国家名,按其字母的顺序输出。


255 8

const name:array[1..10] of string[20]=( 'China', 'France', 'Canada', 'Australia', 'Spain', 'American', 'Sweden', 'Poland', 'Turkey', 'Japan');

var i,j:integer;t:string[20];

Begin

for i:=1 to 9 do

for j:=i+1 to 10 do

if name[i]>name[j] then

begin

t:=name[i];name[i]:=name[i];name[i]:=t

end;

for i:=1 to 10 do writelname[i]);

end.


255 8

  • 例4、有一个四位数①它是一个完全平方数②千位数和百位数相等,十位数和个位数相等。求这个四位数。


255 8

var m,n:integer;

st:string[4];

begin

for n=32 to 99 do

begin

m:=n*n; str(m,st);

if (copy(st,1,1)=copy(st,2,1)) and

(copy(st,3,1)=copy(st,4,1))

then writeln(m)

end

end.


255 8

例7.23 对输入的一句子实现查找且置换的功能。分析:程序中,输入要查找的字符串及要置换的字符串,充分用上了字符串处理的标准过程delete、insert及标准函数pos。


255 8

  程序如下:program exp7_23;vars1,s,o:string;i:integer;beginwrite('The text:');readln(s1);write('Find:');readln(s);write('Replace:');readln(o);i:=pos(s,s1);while i<>0 do begindelete(s1,i,length(s));insert(o,s1,i);i:=pos(s,s1);end;writeln(s1);readln;end.


255 8

字符串应用

  • 输入一行字符,包含若干个单词。约定相邻的两个单词用空格隔开,编程统计单词的个数。

  • 分析:先将所有字符存储在一个字符串st中,然后通过对st的扫描及对空格字符的判断进行统计单词。


255 8

参考程序

program check;

var st:string;

i,l,num:integer;

begin

writeln('input the charactors:');

readln(st);

l:=length(st);

i:=1;num:=0;

while i<l do

begin

while st[i]=' ' do i:=i+1;

if i<=l then num:=num+1;

while (st[i] <> ' ') and (i<l) do i:=i+1;

end;

writeln('total:',num:3);

end.


255 8

字符串应用

  • 输入两个整数x,y,输出它们的和。(0≤x,y≤10100)

  • 分析:处理的数据x,y的范围远远超过了整数、实数所能承受的最大范围,只能采用字符串进行处理。


255 8

参考程序

program sum;

var st:string;

x,y:array[0..101] of integer;

i,l1,l2:integer;

begin

for i:=0 to 101 do begin x[i]:=0; y[i]:=0; end;

write('x='); readln(st);

l1:=length(st);

for i:=l1 downto 1 do x[l1-i]:=ord(st[i])-ord('0');

write('y='); readln(st);

l2:=length(st);

for i:=l2 downto 1 do y[l2-i]:=ord(st[i])-ord('0');

if l1<l2 then l1:=l2;

for i:=0 to l1 do

begin

x[i]:=x[i]+y[i];

x[i+1]:=x[i+1] + x[i] div 10;

x[i]:=x[i] mod 10;

end;

write('x+y=');

l1:=l1+1;

while x[l1]=0 do l1:=l1-1;

for i:=l1 downto 0 do write(x[i]);

readln;

end.


  • Login