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

1 / 18

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

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），统计该单词在句子中出现的次数

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

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

• Begin

• 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.

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

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

Begin

max:=0; l:=0;

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.

Var wd:string[30];

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

Begin

for i:=1 to n do

begin

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,’%’);

End.

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.

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

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.

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

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.

### 字符串应用

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

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

### 参考程序

program check;

var st:string;

i,l,num:integer;

begin

writeln('input the charactors:');

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.

### 字符串应用

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

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

### 参考程序

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;

l1:=length(st);

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

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]);