1 条题解

  • 0
    @ 2021-6-14 23:25:16

    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;
    }
    

    Pascal :

    var
      s,t,s1:string;
      i:integer;
      a,b,c:char;
    begin
      readln(s);
      s1:=s;
      readln(t);
      if t[1]='D' then begin
        i:=pos(t[3],s);
        delete(s,i,1);
      end else
        if t[1]='I' then begin
          for i:=length(s) downto 1 do if s[i]=t[3] then break;
            insert(t[5],s,i);
        end else
          begin
            for i:=1 to length(s) do if s[i]=t[3] then s[i]:=t[5];
          end;
      if s1=s then writeln('Not exist') else writeln(s);
    end.
    
    

    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");
    		
    	}
    }
    
    
    • 1

    信息

    ID
    180
    时间
    1000ms
    内存
    125MiB
    难度
    10
    标签
    递交数
    10
    已通过
    2
    上传者