/* Name: ex8-6.c Purpose: B1FF filter : practice with one dimensional arrays Author: Bill Slough Notes: Improved solution: introduces functions to the previous solution. */ #include #include #include #define N 80 /* maximum length of input line */ int getline(char a[ ], int size, bool *overflow); void putline(char a[ ], int n); void translate(char a[ ], int n); int main() { char line[N]; /* holds one line of input */ int lineLength; /* number of characters in the line */ bool lineOverflow; /* too many characters input? */ /* Get a line of input, being careful not to overfill the buffer */ printf("Enter message: "); lineLength = getline(line, N, &lineOverflow); /* Terminate execution if there were too many input characters */ if (lineOverflow) { printf("Error in input: buffer overflow.\n"); return 1; } /* translate and output the coded message */ translate(line, lineLength); printf("In B1FF speak: "); putline(line, lineLength); printf("!!!!!!!!!!\n"); return 0; } /* Read a line of characters into the array a, which has capacity = size */ int getline(char a[ ], int size, bool *overflow) { int i; char ch; i = 0; while (i < size && (ch = getchar()) != '\n') { a[i++] = ch; } *overflow = (i == size) && (getchar() != '\n'); return i; } /* Output the n characters stored in a given array */ void putline(char a[ ], int n) { int i; for (i = 0; i < n; i++) printf("%c", a[i]); } /* Replace the n characters stored in a given array, using the B1FF rules */ void translate(char a[ ], int n) { char ch; int i; for (i = 0; i < n; i++) { ch = toupper(a[i]); switch(ch) { case 'A': ch = '4'; break; case 'B': ch = '8'; break; case 'E': ch = '3'; break; case 'I': ch = '1'; break; case 'O': ch = '0'; break; case 'S': ch = '5'; break; } a[i] = ch; } }