/**************************************************** * ACS Computing Competition July 1995. * * C solution to * PracticeProblem E3 : String Processing * * Brendan Humphreys. * brendan@cssa.anu.edu.au * * */ #include <stdio.h> #include <stdlib.h> #define GOSTART '0' /* command char macros */ #define GOEOS '$' #define DEL 'x' #define SWAP 's' #define INSERT 'i' #define UPPER 'u' #define LEFT '-' #define RIGHT '+' #define EOL '\n' #define EOS '\0' #define TERMCH "#" char string[100]; /* the input line */ char temp[100]; /* temp storage for insert operation */ void main() { int ch; int done; char *cursor, *start, *end; /* pointers to parts of the input string */ while(1) { gets(string); /* get the input string from stdin */ if (strcmp(string,TERMCH) == 0) /* catch the terminating condition */ exit(0); cursor = start = string; end = string + strlen(string) - 1; /* end points to last char in string */ done = 0; while (!done) { ch = getchar(); /* get next command */ switch (ch) { case GOSTART: cursor = start; /* cursor now points to start of string */ break; case GOEOS: cursor = end+1; /* cursor now points to EOS */ break; case DEL: if (cursor <= end) { *cursor = EOS; /* replace char with EOS */ strcat(string,(char *)(cursor+1)); /* concatenate strings */ end--; } break; case SWAP: if (cursor < end) /* this op not allowed if near EOS */ { ch = *cursor; /* get char at cursor */ *cursor = *(cursor+1); /* copy near right char */ *(cursor+1) = ch; /* put char in its place */ } break; case INSERT: ch = getchar(); /* get char to insert */ strcpy(temp,cursor); /* save second half of string */ *cursor = ch; /* add the char */ *(cursor+1) = EOS; /* terminate the first half */ strcat(string,temp); /* join the halves */ cursor++; /* move cursor to right one place */ end++; break; case UPPER: *cursor = toupper(*cursor); /* convert to upper case */ if (cursor <= end) cursor++; /* move cursor to right one place */ break; case LEFT: if (cursor > start) /* don't allow if at start of string */ cursor--; /* move cursor one place left */ break; case RIGHT: if (cursor <= end) /* don't allow if at end of string */ cursor++; /* move cursor one place right */ break; case EOL: done = 1; /* all done... */ break; default: printf("Unknown command '%c' \n",ch); exit(1); } } printf("%s\n",string); } }