Permalink
Browse files

Ch 14 ex 13 - Switch statement.

  • Loading branch information...
1 parent 3dadfb4 commit bad6ae20ac6b1f69902f7afe58fadd89137af867 Toon Ketels committed Apr 14, 2012
Showing with 68 additions and 2 deletions.
  1. +2 −2 Makefile
  2. BIN ex13
  3. +66 −0 ex13.c
View
@@ -10,10 +10,10 @@ CFLAG=-Wall -g
# clean
# distclean
-all: ex1 ex3 ex5 ex6 ex7 ex8 ex9 ex10 ex11 ex12
+all: ex1 ex3 ex5 ex6 ex7 ex8 ex9 ex10 ex11 ex12 ex13
clean:
- rm -f ex1 ex3 ex5 ex6 ex7 ex8 ex9 ex10 ex11 ex12
+ rm -f ex1 ex3 ex5 ex6 ex7 ex8 ex9 ex10 ex11 ex12 ex13
list:
ls
View
BIN ex13
Binary file not shown.
View
66 ex13.c
@@ -0,0 +1,66 @@
+#include <stdio.h>
+
+/**
+ * http://c.learncodethehardway.org/book/learn-c-the-hard-waych14.html
+ *
+ * Switch statements ~ Jump tables,
+ * only expressions that evaluate to integers.
+ */
+
+
+/**
+ * Custom tolower function.
+ */
+char _tolower(char character) {
+ return (character >= 'A' && character <= 'Z') ? character - ('A' - 'a') : character;
+}
+
+
+int main(int argc, char *argv[])
+{
+ if(argc != 2) {
+ printf("ERROR: provide one argument!\n");
+ // Abort program
+ return 1;
+ }
+
+ int i = 0;
+ for(i; argv[1][i] != '\0'; i++) {
+ char letter = _tolower(argv[1][i]);
+
+ switch(letter) {
+ case 'a':
+ //case 'A':
+ printf("%d: 'A'\n", i);
+ break;
+ case 'e':
+ //case 'E':
+ printf("%d: 'E'\n", i);
+ break;
+ case 'i':
+ //case 'I':
+ printf("%d: 'I'\n", i);
+ break;
+ case 'o':
+ //case 'O':
+ printf("%d: 'O'\n", i);
+ break;
+ case 'u':
+ //case 'U':
+ printf("%d: 'U'\n", i);
+ break;
+ case 'y':
+ //case 'Y':
+ if(i > 2) {
+ printf("%d: 'V'\n", i);
+ break;
+ }
+ default:
+ printf("%d: %c is not a vowel\n", i, letter);
+
+ }
+ }
+
+ return 0;
+}
+

0 comments on commit bad6ae2

Please sign in to comment.