Why does my program ignore arguments after #?

Viewed 153

I'm going through Zed Shaw's 'Learn C The Hard Way' (LCTHW), and today I'm doing exercise 14. Exercise 14 is a simple program that prints the ASCII codes of the characters of each argument that you give when you run the program. You can view my code here (please excuse the random comments; I use this repo only to store my LCTHW notes).

My program is not running entirely as expected. I am calling the program using Terminal on OSX, and the program works fantastically until I run it using an argument that begins with '#' - that argument is ignored, as well as any after. For example, running the following command in terminal:

./ex14 Hello Simon #num53 okay 

will provide only the ASCII codes for Hello Simon, but not for #num53 or okay, i.e. -

LCTHW-sols $ ./ex14 Hello Simon #num53 okay
Hello has 5 chars
'H' == 72 'e' == 101 'l' == 108 'l' == 108 'o' == 111 
--------------
Simon has 5 chars
'S' == 83 'i' == 105 'm' == 109 'o' == 111 'n' == 110 
--------------

Why is this? Further, how can I change my code so that it will accept any argument?

ex14.c copied below without my notes comments -

#include <stdio.h>
#include <ctype.h> // Functions to classify and transform chars
#include <string.h> // for strlen()

void print_letters(char arg[]);

void print_arguments(int argc, char *argv[])
{
        int i = 0;

        for(i = 1; i < argc; i++) { 
                 print_letters(argv[i]);
        }
} // end print_arguments

void print_letters(char arg[])
{
        printf("%s has %lu chars\n", arg, strlen(arg));
        int i = 0;
        for(i = 0; arg[i] != '\0'; i++) {
                char ch = arg[i];

                // Shouldn't differ the print_arguments loop
                if(isalpha(ch) || isblank(ch) || isdigit(ch)) {
                        printf("'%c' == %d ", ch, ch);
                }
        }

        printf("\n--------------\n");
}

int main(int argc, char *argv[])
{
        print_arguments(argc, argv);
        return 0;
}
1 Answers
Related