/* This file contains the fragments of code from the lectures on chars and strings, for you to play around with. */ #include #include #include #include /* we use this in several places */ #define NUMLETS 26 /* here is the roman number function repeated from roman.c */ void PrintNum(int n) { while (n > 0) { if (n >= 100) { n = n - 100; putchar('C'); } else if (n >= 90) { n = n + 10; putchar('X'); } else if (n >= 50) { n = n - 50; putchar('L'); } else if (n >= 40) { n = n + 10; putchar('X'); } else if (n >= 10) { n = n - 10; putchar('X'); } else if (n >= 9) { n = n + 1; putchar('I'); } else if (n >= 5) { n = n - 5; putchar('V'); } else if (n >= 4) { n = n + 1; putchar('I'); } else { n = n - 1; putchar('I'); } } } /* This function Caesar-enciphers standard input to standard output */ /* how much to rotate the alphabet: A -> N, B -> O, and so on. */ const int OFFSET = 13; void CaesarCipher (void) { int c, ord; /* Why is c declared as int and not char? */ while ((c = getchar()) != EOF) { c = toupper(c); if (isupper(c)) { ord = c - 'A'; /* Integer in range [0,25] */ ord = (ord + OFFSET) % NUMLETS; /* permute by offset */ c = ord + 'A'; /* back to char */ } putchar(c); } } /* This function counts the frequencies of letters in the standard input, and prints the results to standard output. */ void CountLetters(void) { int c, i, count[NUMLETS]; for (i = 0; i < NUMLETS; i++) count[i] = 0; while ((c = getchar()) != EOF) { c = toupper(c); if (isupper(c)) { i = c - 'A'; /* Integer in [0,25] */ count[i]++; } } for (i = 0; i < NUMLETS; i++) { printf("%c: %d\n", i + 'A', count[i]); } } /* This function gives a count of the length of each line on standard input, echoing the line followed by the count to standard output. */ void LineLengths(void) { int c, charCount = 0, lineCount = 0; while ((c = getchar()) != EOF) { if (c == '\n') { lineCount++; printf(" [Line %d has %d characters]\n", lineCount, charCount); charCount = 0; } else { charCount++; putchar(c); } } } int main(void) { /* Write your own main routine to call those functions */ return EXIT_SUCCESS; }