The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 7.01 on page 153
Write a program that converts upper case to lower or lower case to upper, depending on the name it is invoked with, as found in argv[0].
Solution by Richard Heathfield
/* This program converts its input to upper case * (if argv[0] begins with U or u) or lower case. * If argc is 0, it prints an error and quits. */ #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main(int argc, char **argv) { int (*convcase[2])(int) = {toupper, tolower}; int func; int result = EXIT_SUCCESS; int ch; if(argc > 0) { if(toupper((unsigned char)argv[0][0]) == 'U') { func = 0; } else { func = 1; } while((ch = getchar()) != EOF) { ch = (*convcase[func])((unsigned char)ch); putchar(ch); } } else { fprintf(stderr, "Unknown name. Can't decide what to do.\n"); result = EXIT_FAILURE; } return result; }
Here's a category 1 solution from Bryan Williams...
/* Exercise 7-1. Write a program that converts upper case to lower case or lower case to upper, depending on the name it is invoked with, as found in argv[0]. Assumptions: The program should read from stdin, until EOF, converting the output to stdout appropriately. The correct outputs should be : Program Name Output lower stdin with all caps converted to lower case upper stdin with all lowercase characters converted to uppercase [anything else] helpful message explaining how to use this Author : Bryan Williams */ #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define SUCCESS 0 #define NO_ARGV0 1 #define BAD_NAME 2 int main(int argc, char *argv[]) { int ErrorStatus = SUCCESS; int (*convert)(int c) = NULL; int c = 0; /* check that there were any arguments */ if(SUCCESS == ErrorStatus) { if(0 >= argc) { printf("Your environment has not provided a single argument for the program name.\n"); ErrorStatus = NO_ARGV0; } } /* check for valid names in the argv[0] string */ if(SUCCESS == ErrorStatus) { if(0 == strcmp(argv[0], "lower")) { convert = tolower; } else if(0 == strcmp(argv[0], "upper")) { convert = toupper; } else { printf("This program performs two functions.\n"); printf("If the executable is named lower then it converts all the input on stdin to lowercase.\n"); printf("If the executable is named upper then it converts all the input on stdin to uppercase.\n"); printf("As you have named it %s it prints this message.\n", argv[0]); ErrorStatus = BAD_NAME; } } /* ok so far, keep looping until EOF is encountered */ if(SUCCESS == ErrorStatus) { while(EOF != (c = getchar())) { putchar((*convert)(c)); } } /* and return what happened */ return SUCCESS == ErrorStatus ? EXIT_SUCCESS : EXIT_FAILURE; }










