In this problem we simulate a text editor. Given a series of keyboard input, output the final text content.
The text editing rules are defined as follows:
The size of the text content is fixed to 500 characters, and the text content of test cases will not exceed 500 characters when simulating.
Example 1:
Input:
aaaaa/naaaaa/naaa/l/ubbbccccc/u/u/uddd
Output:
aaaaaddd
aabbbcccccaaa
aaa
Example 2:
Input:
edcba/c/l/l/l/p/s/c/p/pzzz
Output:
edabcabczzzabc
Hint:
The 1st test case includes “/l”, “/s”.
The 2nd test case includes “/r”, “/l”, “/n”, “/b”.
The 3rd and 4th test cases include “/s”, “/r”, “/l”, “/n”, “/b”.
The 5th and 6th test cases include “/s”, “/r”, “/l”, “/n”, “/b”, “/c”, “/p”.
The 7th and 8th test cases include “/s”, “/r”, “/l”, “/n”, “/b”, “/c”, “/p”, “/u”.
Hint2: You can use the following code to read the input.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_SIZE 501
char input[MAX_SIZE] = {0};
// It will be easier to manipulate the string in a 1D character array
char content[MAX_SIZE] = {0};
int main() {
gets(input);
int i;
for (i = 0; i < MAX_SIZE; i++) {
if (input[i] == '/') {
if (input[i + 1] == 'b') {
i++;
// Your code here
} else if (input[i + 1] == 'n') {
i++;
// Your code here
} else if (input[i + 1] == 'l') {
i++;
// Your code here
} else if (input[i + 1] == 'r') {
i++;
// Your code here
} else if (input[i + 1] == 'u') {
i++;
// Your code here
} else if (input[i + 1] == 'c') {
i++;
// Your code here
} else if (input[i + 1] == 'p') {
i++;
// Your code here
} else if (input[i + 1] == 's') {
i++;
// Your code here
}
} else if (isalpha(input[i]) || input[i] == ' ') {
// Your code here
}
}
printf("%s", content);
return 0;
}
The keyboard input sequence.
There is always a valid command(/b /n /l /r /u /c /p /s) right after the backslash character.
There is no newline character at the end.
The final text content.