主题:我有道——字符串编辑做不出 有谁能帮我做
rxy [专家分:0] 发布于 2005-07-27 19:06:00
字符串编辑:
从键盘输入一个字符串(长度<=40个字符),并以字符'.'结束.
例如:'This is a book.',现对该字符串进行编辑,编辑功能有:
D:删除一个字符,命令的方式为:
D a 其中a为被删除的字符
例如:D s 表示删除字符's',若字符串中有多个's',则删除第一次出现的,如上例中删除的结果为:
'Thi is a book.'
I:插入一个字符,命令的格式为:
I a1 a2 其中a1表示插入到指定字符前面,a2表示将要插入的字符
例如: I s d 表示在指定字符's'的前面插入字符'd',若原串中有多个's',则插入在最后一个字符的前面,
如上例中,原串:'This is a book.'
插入后:'This ids a book.'
R:替换一个字符,命令格式为:
R a1 a2 其中a1为被替换的字符,a2为替换的字符,若在原串中有多个a1,则应全部替换
例如:原串:'This is a book.'
输入命令: R o e
替换后:' This is a beek.'
Input
该题有多组测试数据,第一行为一个整数N,表示N组测试数据.
接下去N*2行,每组数据包含两行,第一行为原串以'.'结尾,第二行为编辑命令,格式如题目描述。
Output
每组数据输出编辑后的新串,如果未找到指定字符则输出error。
注:当操作为R时,如果未找到指定字符,不属于error。
Sample Input
3
This is a book.
R o e
This is a book.
R t e
This is a book.
D c
Sample Output
This is a beek.
This is a book.
error
回复列表 (共9个回复)
沙发
HRQ [专家分:60] 发布于 2005-07-29 21:25:00
program abc
板凳
口口and枕头 [专家分:1550] 发布于 2005-07-30 00:45:00
1楼的就这样了吗?
3 楼
口口and枕头 [专家分:1550] 发布于 2005-07-30 00:48:00
其实我觉得简单,写出来太烦`~~~就是练习一下函数的定义`~~~~~
在用CASE语句对应一下就可以了~~~~
是这样吗?
4 楼
sd5774188 [专家分:260] 发布于 2005-07-30 13:27:00
program yb(input,output);
const max=40;
var s:string[max];
s1:string[5];
n:integer;
procedure dm(ch:char);
begin
if pos(ch,s)=0 then writeln('error')else begin
delete(s,pos(ch,s),1);
writeln(s);
end;
end;
procedure im(ch,ch1:char);
var i:byte;
begin
if pos(ch,s)=0 then writeln('error')
else for i:=length(s)downto 1 do
if s[i]=ch then
begin
insert(ch1,s,i);
writeln(s);
break;
end;
end;
procedure rm(ch,ch1:char);
begin
while pos(ch,s)<>0 do s[pos(ch,s)]:=ch1;
writeln(s);
end;
begin
readln(n);
while n<>0 do
begin
dec(n);
readln(s);
readln(s1);
if s1[1]='D' then dm(s1[3])
else if s1[1]='I' then im(s1[3],s1[5])
else rm(s1[3],s1[5]);
end;
end.
5 楼
stuart920106 [专家分:730] 发布于 2005-07-31 23:21:00
pascal也用到break????(C++的把)(指教一下)
6 楼
MagicG [专家分:650] 发布于 2005-08-03 21:05:00
对啊,PASCAL也用的啊,意思是跳出当前循环(一层),C++里是什么意思啊?偶不知道
7 楼
weiyulan [专家分:50] 发布于 2005-08-07 14:52:00
[fly]C++是C语言的增强[/fly]
[fly] C++是C语言的增强[/fly]
[fly] C++是C语言的增强[/fly]
[fly] C++是C语言的增强[/fly]
[fly] C++是C语言的增强[/fly]
8 楼
QQ331373582 [专家分:1500] 发布于 2005-08-08 10:00:00
break=跳出循环
9 楼
smartq [专家分:80] 发布于 2005-10-04 10:54:00
我认为这样:才最简便:
program asdf;
var
begin
read(s);
read(ch,ch1);
n:=length(s);
for i:=1 to n do
begin
if ch=d then if s[i]=ch1 then s[i]:=0;
end;
write(s);
end.
{本程序只能实现一种功能,其他都相似}
我来回复