Thread: Zed Shaw Learn C The Hard Way - Extra Credit 14.3 questions 2 and 3.

  1. #1
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42

    Zed Shaw Learn C The Hard Way - Extra Credit 14.3 questions 2 and 3.

    Hi
    Just needed to post a couple of exercises im struggling on from Learn C the Hard Way - exercise 13: Exercise 13: Switch Statement
    The "extra credit" Im not sure about is:
    • Use the ',' (comma) to initialise letter in the for-loop. &
    • Make it handle all of the arguments you pass it with yet another for-loop.

    The code I've come up with is below I hope its readable....
    I can understand how to make it handle a second argument but not an indefinite amount. I also can't understand how to initialise Letter in the for-loop using comma.

    If theres somebody who doesnt mind explaining this to me I would very much appreciate it.

    Thanks very much

    Code:
    #include <stdio.h>
    int main(int argc, char *argv[])
     {
        if(argc < 2) {
            printf("ERROR: You need one argument.\n"); // this is how you abort a program
            return 1;
        }
    
    
        int i = 0;
        for(i = 0; argv[1][i] != '\0'; i++) {
            char letter = 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) {
                    // it's only sometimes Y
                     printf("%d: 'Y'\n", i);
                }
                break;
    
    
            default:
                printf("%d: %c is not a vowel\n", i, letter);
            }
        }
        if(argc >= 2) {    
            for(i = 0; argv[2][i] != '\0'; i++) {
                char letter = argv[2][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) {
                        // it's only sometimes Y
                         printf("%d: 'Y'\n", i);
                    }
                    break;
    
    
                default:
                    printf("%d: %c is not a vowel\n", i, letter);
                }
            }
        }    
    
    
        
        return 0; 
    }

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    The first requirement is just telling you to change one line, from what i understand (even though i fail to see the reason):
    Code:
    char letter = ',';
    For the second requirement, if you look how you had to deal with the second argument, you repetead almost the same code to deal with the first one, except only for one thing you incremented.
    You can write that code once and let the for loop increment that "thing", up to the number of arguments you currently have.
    Last edited by Smjert; 08-24-2014 at 03:16 PM.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Quote Originally Posted by Smjert View Post
    The first requirement is just telling you to change one line, from what i understand (even though i fail to see the reason):
    Code:
    char letter = ',';
    I suggest you both google on the for loop, or get a good book which will tell you how to use a comma within the for statement.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,646
    A comma allows you to specify multiple statements, and can be used just about anywhere, including in this case as one of the parameters of a for loop.
    Code:
    int i, j, k;
    /* ... */
        i = 1, j = 2, k = 3;

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by gemera View Post
    I suggest you both google on the for loop, or get a good book which will tell you how to use a comma within the for statement.
    I think it was only a language problem, i very well know about the comma in the for statement but when i read that in, i though about letter character that was already inside the body of the for loop.
    So basicly i read that as "initialize letter with the comma character". :P.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learn C by Hard Way vs The C programming Language
    By Mihai Feraru in forum C Programming
    Replies: 3
    Last Post: 04-15-2014, 05:57 PM
  2. Learn C The Hard Way
    By talin in forum C Programming
    Replies: 4
    Last Post: 06-03-2013, 12:06 AM
  3. Help - Car Rite Rental (extra credit)
    By mmourot in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2011, 07:00 AM
  4. Please help me with this Extra Credit program
    By DenisGFX in forum C Programming
    Replies: 10
    Last Post: 05-19-2002, 01:48 AM
  5. is C++ really hard to learn
    By Shy_girl_311 in forum C++ Programming
    Replies: 11
    Last Post: 11-11-2001, 12:15 PM

Tags for this Thread