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