2327 - 字符串编辑

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 125 MB

从键盘输入一个字符串(长度<=40个字符),并以字符 '.'结束。

例如:'This is a book.' 现对该字符串进行编辑,编辑功能有:

D:删除一个字符,命令的方式为:

     D a  其中a为被删除的字符

     例如:D s  表示删除字符 's' ,若字符串中有多个 's',则删除第一次出现的。

              如上例中删除的结果为: 'Thi is a book.'


<span>I</span>:插入一个字符,命令的格式为:<span></span> 

<span>&nbsp;&nbsp;&nbsp; I a1 a2&nbsp; </span>其中<span>a1</span>表示插入到指定字符前面,<span>a2</span>表示将要插入的字符。<span></span> 

&nbsp; &nbsp; 例如:<span>I s d&nbsp; </span>表示在指定字符<span>&nbsp;'s'&nbsp;</span>的前面插入字符 '<span>d'&nbsp;</span>,若原串中有多个<span>&nbsp;'s'&nbsp;</span>,则插入在最后一个字符的前面。

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;如上例中:<span><br />

                   原串:'This is a book.'

<span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>插入后:'<span>This ids a book.'</span> 

<span>&nbsp;</span> 

<span>R</span>:替换一个字符,命令格式为:<span></span> 

&nbsp; &nbsp; &nbsp;R a1 a2&nbsp; 其中a1为被替换的字符,a2为替换的字符,若在原串中有多个a1则应全部替换。

&nbsp; &nbsp; &nbsp;例如:

原串: 'This is a book.'

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;输入命令:<span>R o e</span> 

<span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>替换后的字符串为:<span>'This is a beek.'</span> 

<span><br />

在编辑过程中,若出现被改的字符不存在时,则给出提示信息"Not exist"。

<br />

题目输入

每个测试文件只包含一组测试数据,每组输入数据包含两行:

第一行,输入一个字符串,表示原串;

第二行,输入一个字符串,表示命令。


题目输出

对于每组输入数据,输出编辑后的字符串,如果被改的字符不存在,则输出"Not exist"(引号不输出)。


输入/输出样例

输入格式

This is a book.
D s

输出格式

Thi is a book.

C语言解答

#include<stdio.h>
#include<string.h>
#define N 45
int main()
{
	char a[N],b[10];
	int i,flag=0,len,m=1,j;
	gets(a);
	gets(b);
	len=strlen(a);
	if(b[0]=='D'){
		for(i=0;a[i]!='\0';i++){
			if(a[i]==b[2]) flag=1;
		}
		if(flag==1){
			for(i=0;a[i]!='\0';i++){
				if(m==1 && a[i]==b[2]){
					m=0;
					continue;
				}
				else{
					printf("%c",a[i]);
				}
			}
		}
		else{
			printf("Not exist");
		}
		printf("\n");
	}
	else if(b[0]=='I'){
		for(i=len-1;i>=0;i--){
			if(a[i]==b[2]){
				flag=1;
				break;
			}
		}
		if(flag==1){		
			for(j=0;j<i;j++){
				printf("%c",a[j]);
			}
			printf("%c",b[4]);
			for(j=i;a[j]!='\0';j++){
				printf("%c",a[j]);
			}
		}
		else printf("Not exist");
		printf("\n");
	}
	else{
		for(i=0;a[i]!='\0';i++){
			if(a[i]==b[2]) flag=1;
		}
		if(flag==1){
			for(i=0;a[i]!='\0';i++){
				if(a[i]==b[2]){
					printf("%c",b[4]);
				}
				else{
					printf("%c",a[i]);
				}
			}
		}
		else{
			printf("Not exist");
		}
		printf("\n");
	}
} 

C++解答

#include<stdio.h>
#include<string.h>
int main()
{
	char a[110],b[110],ch,q,p;
	int i,count,j;
		for(i=0;;i++)
		{
			scanf("%c",&a[i]);
			if(a[i]=='.')
				break;
		}
		a[i+1]='\0';
		getchar();
		scanf("%c",&ch);
		getchar();
		if(ch=='D')
			scanf("%c",&q);
		else
			scanf("%c %c",&q,&p);
		getchar();
		if(ch=='D')
		{
			count=0,j=0;
			for(i=0;i<strlen(a);i++)
			{
				if(a[i]==q&&count==0)
				{
					count++;
				}
				else
				{
					b[j++]=a[i];
				}
			}
			b[j]='\0';
			if(count==0)
				printf("Not exist\n");
			else
			    printf("%s\n",b);
		}
		if(ch=='I')
		{
			count=0;j=strlen(a);
			for(i=strlen(a)-1;i>=0;i--)
			{
				if(a[i]==q&&count==0)
				{
					count++;
					b[j--]=a[i];
					b[j--]=p;
				}
				else
				{
					b[j--]=a[i];
				}
			}
			b[strlen(a)+1]='\0';
			if(count==0)
				printf("Not exist\n");
			else
			    printf("%s\n",b);
		}
		if(ch=='R')
		{
			count=0,j=0;
			for(i=0;i<strlen(a);i++)
			{
				if(a[i]==q)
				{
					b[j++]=p;
					count++;
				}
				else
					b[j++]=a[i];
			}
			b[j]='\0';
			if(count==0)
				printf("Not exist\n");
			else
				printf("%s\n",b);
		}
	return 0;
}

Java解答


import java.util.Scanner;

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner rd=new Scanner(System.in);
		int flag = 0;
		String str=rd.nextLine();
		String mingling=rd.next();
		String c=rd.next();
		StringEditor ed=new StringEditor(str,c);
		switch (mingling){
		
		case "D":
			ed.Del(str,c);
			break;
		case "I":
			String d = rd.next();
			ed.Insert(str, c, d);
			break;
		case "R":
			String d1 = rd.next();
			ed.Replace(str, c, d1);
			
			break;
		}
	}
}
class StringEditor
{
	private String fString;
	private String order;
	public StringEditor(String fString,String order)
	{
		this.fString=fString;
		this.order=order;
	}
	public void Del(String str,String b)
	{
		if(str.indexOf(b)!=-1)
		{
			str = str.replaceFirst(b,"");
			System.out.print(str);
		}
		else
			System.out.print("Not exist");
	}
	public void Insert(String str,String a1,String a2)
	{
		if(str.indexOf(a1)!=-1){
			
			int i=str.lastIndexOf(a1);
			StringBuilder sb=new StringBuilder(str);
			System.out.print(sb.insert(i, a2));
		}
		else
			System.out.println("Not exist");
	}
	public void Replace(String str,String a1,String a2)
	{
		if(str.indexOf(a1)!=-1)
		{
			String s=str.replaceAll(a1, a2);
			System.out.print(s);
		}
		else
			System.out.println("Not exist");
		
	}
}