Advanced String Editor

Advanced String Editor

Title Description

Keyboard input from a string (length <= 40 characters), and character '.' End. Editing functions:
. 1 D: delete a character mode command is: D a wherein a is deleted characters, for example: D s indicates the deleted character 's', if there are a plurality of 's' string, the first deleted occurrences.
2 I: insert a character, a command format is: I a1 a2 where a1 is inserted into the front of the specified character, a2 represents the character to be inserted. For example: I sd represent insertion character 'd' in front of the specified character 's', if there are a plurality of 's' in the original string is inserted in front of the last character.
3 R: replacing a character, command format is: R a1 a2 where a1 is replaced characters, a2 character Alternatively, if there are a plurality of original strings a1 should replace all.
In the editing process, if the change appears to be a character not present, prompt information ( "no exist").

Entry

The first line of the input line of the second line character string input instruction

this is a book.
D s

Export

After the output string is edited, if not edit the output no exist

thi is a book.

Code

#include<iostream>
#include<string>
using namespace std;

int main()
{
	char ss[42];
	int i=0,t=-1;
	while(1)
	{
		scanf("%c",&ss[i]);
		if(ss[i]=='.') break;
		i++;
	}
	getchar();
	char cc,c1,c2;
	scanf("%c",&cc);
	if(cc=='D')
	{
		getchar();
		scanf("%c",&c1);
		for(int j=0;j<=i;j++)
		{
			if(ss[j]==c1 && t==-1 ) t=j;
		}
		if(t==-1) cout<<"no exist";
		else
		{
			for(int j=0;j<=i;j++)
			{
				if(j!=t) cout<<ss[j];
			}
		}
	}
	else
	{
		getchar();
		scanf("%c %c",&c1,&c2);
		
		if(cc=='R')
		{
			for(int j=0;j<=i;j++)
			{
				if(ss[j]==c1)
				{
					ss[j]=c2;
					t=1;
				}
			}
			if(t!=-1)
			{
				for(int j=0;j<=i;j++)
				{
					cout<<ss[j];
				}
			}
			else cout<<"no exist";
			
		}
		if(cc=='I')
		{
			for(int j=0;j<=i;j++)
			{
				if(ss[j]==c1) 
				{
					t=j; 
				}
			}
			if(t==-1) cout<<"no exist";
			else
			{
				for(int j=0;j<=i;j++)
				{
					if(j==t) cout<<c2;
					cout<<ss[j];
				}
			} 
			
		}
		
	}	
	return 0;
} 
Published 11 original articles · won praise 1 · views 255

Guess you like

Origin blog.csdn.net/weixin_42408097/article/details/104416326