/* piro4.c - Don Yang (uguu.org) 04/08/01 */ #include #include #include #define DICTIONARY_SIZE 15 #define MAX_WORD_LENGTH 8 typedef struct { int c; int ucase; int punct; } Character; char *Dictionary[DICTIONARY_SIZE][2] = { {"computer", "box"}, {"dude", "d00d"}, {"elite", "l33t"}, {"you.", "j00."}, {"the.", "da."}, {"il", "i1"}, {"li", "1i"}, {"s.", "z."}, {"a", "4"}, {"e", "3"}, {"i", "1"}, {"l", "1"}, {"o", "0"}, {"s", "5"}, {"t", "7"} }, *s; Character Stack[MAX_WORD_LENGTH + 1], Buffer[MAX_WORD_LENGTH + 1], c; int BufSize, StackSize, EndOfFile, d, i, x, y; void Convert(Character *dst, int src) { if( !isalpha(src) ) { dst->c = '.'; dst->ucase = 0; dst->punct = src; if( src == EOF ) EndOfFile = 1; } else { dst->c = tolower(src); dst->ucase = isupper(src); dst->punct = 0; } } int Compare(void) { for(i = 0; i < BufSize; i++) { Convert(&c, Dictionary[d][0][i]); if( !Dictionary[d][0][i] || Buffer[i].c != c.c ) return 0; } return 1; } void ExactMatch(void) { for(d = 0; d < DICTIONARY_SIZE; d++) { if( strlen(Dictionary[d][0]) == (size_t)BufSize && Compare() ) return; } d = -1; } void NextChar(void) { if( StackSize ) { Buffer[BufSize].c = Stack[--StackSize].c; Buffer[BufSize].ucase = Stack[StackSize].ucase; Buffer[BufSize].punct = Stack[StackSize].punct; } else { Convert(&Buffer[BufSize], EndOfFile ? EOF : getchar()); } } void Output(void) { s = Dictionary[d][1]; if( Buffer[0].punct ) putchar(Buffer[0].punct); else putchar(Buffer[0].ucase ? toupper(s[0]) : s[0]); if( strlen(s) > 1 ) { y = Buffer[1].ucase; for(x = 1; x < strlen(s) - 1; x++) putchar(y ? toupper(s[x]) : s[x]); if( Buffer[BufSize - 1].punct ) putchar(Buffer[BufSize - 1].punct); else putchar(y ? toupper(s[strlen(s) - 1]) : s[strlen(s) - 1]); } } int main(void) { StackSize = BufSize = EndOfFile = 0; for(NextChar(); BufSize++ || StackSize || !EndOfFile; NextChar()) { for(d = y = 0; d < DICTIONARY_SIZE; d++) { if( Compare() ) { y++; x = d; } } d = x; if( y == 1 ) { if( strlen(Dictionary[d][0]) == (size_t)BufSize ) { Output(); BufSize = 0; } } else if( !y ) { while( BufSize > 1 ) { Stack[StackSize].c = Buffer[--BufSize].c; Stack[StackSize].ucase = Buffer[BufSize].ucase; Stack[StackSize++].punct = Buffer[BufSize].punct; ExactMatch(); if( d > -1 ) { Output(); BufSize = 0; break; } } if( BufSize == 1 ) { ExactMatch(); if( d > -1 ) { Output(); } else if( Buffer[0].c != EOF ) { if( Buffer[0].punct ) putchar(Buffer[0].punct); else putchar( Buffer[0].ucase ? toupper(Buffer[0].c) : Buffer[0].c); } BufSize = 0; } } } return 0; }