1074 字串編輯

來源:互聯網
上載者:User
 
描述

從鍵盤輸入一個字串(長度<=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.'
在編輯過程中,若出現被指定的字元不存在時,則給出提示資訊'not valid.'

輸入

從鍵盤輸入一個字串(長度<=40個字元),並以字元'.'結束. 後面接著一條命令 含有多組測試資料

輸出

輸出修改後的字串

範例輸入This is a book.
R o e
This is a book.
R m e範例輸出

This is a beek.
not valid.

解題思路:
該題實在變態,用C/C++過不了,居然要用FPC過。簡單的替換和轉換,但就是不知道BT用例。算了,不做解釋,FPC看不懂。

program BUAA1074;vars:string;c,x,y:char;i:integer;procedure error;beginwriteln('not valid.');end;beginrepeatreadln(s);read(c);case c of'D':beginreadln(c,x);if(pos(x,s)=0)then begin error;continue; endelse delete(s,pos(x,s),1);end;'I':beginreadln(c,x,c,y);if(pos(x,s)=0)then begin error;continue; endelse begini:=length(s);while(s[i]<>x)do dec(i);insert(y,s,i);end;end;'R':beginreadln(c,x,c,y);if(pos(x,s)=0)then begin error;continue; endelsefor i:=1 to length(s) doif(s[i]=x)then s[i]:=y;end;end;writeln(s);until eof;end.

 

這是我一直顯示運行錯誤的C程式,囧:

#include <stdio.h>#include <stdlib.h>#include <string.h>void main(){  char s[50],m[1000],n[1000],type,b[10];int i,j,t=0,r=0,count=0,flag=0;char m1,n1,temp1,temp2,l;gets(s);/*讀入資料的處理*/while(gets(b) != NULL){    r=strlen(s);type=b[0];if(type=='D'){ flag=0; n1=b[2]; for(i=0;s[i]!='.';i++) { if(s[i]==n1) {    flag=1;  for(j=i;s[j]!='.';j++)  s[j]=s[j+1];   s[r-1]='\0';  break;  }  } if(flag==0) printf("not valid.\n"); }if(type=='I'){flag=0;   m1=b[2];n1=b[4]; for(i=r;i>=0;i--) { if(s[i]==m1) {   flag=1; for(j=r+1;j>=i;j--) s[j]=s[j-1]; s[i]=n1; s[r+2]='\0'; break; } } if(flag==0) printf("not valid.\n"); }if(type=='R'){  flag=0;m1=b[2];n1=b[4];for(i=0;i<=r;i++){    if(s[i]==m1){flag=1;s[i]=n1;}}if(flag==0)printf("not valid.\n");}if(flag!=0){for(i=0;s[i]!='\0';i++)printf("%c",s[i]);printf("\n");}}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.