I hope I can clearly state my problem here.
I’m on another "extra credit" exercise of Zed Shaws Learn C the Hard Way. It asked to use fewer functions than what the exercise originally starts with hinting at the relevance of the "can_print_it" function. So I've commented out the original code at tried to work how to reduce it to just the functions "print_arguments" and "Print_letters". I’m going to copy and paste the second question of the exercise, it says:
2. Have print_arguments figure how long each argument string is using the strlen function, and then passthat length to print_letters. Then, rewrite print_letters so it only processes this fixed length anddoesn’t rely on the '\0' terminator.
So again I've commented out any original code so its all still there and tried to use the strlen function.
I've approached this by writing out in normal English to myself on some A4 what I think is happening throughout the chain of functions and I was going to type this out below if it not to tedious for people so I can be chased up on any incorrect logic.
The main function takes the argument count and argument vector. This is passed to the function print arguments which takes the arguments in the same way passing the the values through a for loop. The print_letters function is called at the end of this for-loop the argument of print_letters function "argv[i]" is counting through the characters.
At this point I've commented it out for now but this is where I've tried to use strlen. When you uncomment this part is does actually print out the length of each string (vector) but when I try to replace the '\0' character in the for-loop in print_letters with the variable length its quite frankly doesn't work! Is that because its out of scope?.....The last part you can see I've taken what "can_print_it" was doing and tried to place that with the for-loop of the print_letters function but it doesn't manage to print anything out, I thats what I cant work out.
So just to clarify
- I can't work out how to send to values of the variable length in print_arguments function to replace the null character in the for-loop of the print_letters function.
- I can't see why the printf statement at the end of the for-loop in the print_letters function doesn't print out what I'm trying to return from isalpha || isblank.
Sorry about the length of this post I’m keen to understand this so I wanted to make sure I can explain everything I needed.
Thank you very much in advance!
Code:#include <stdio.h>#include <ctype.h> // forward declarations //int can_print_it(char ch); int print_letters(char arg[]); void print_arguments(int argc, char *argv[]) { int i = 0; int length; for(i = 0; i < argc; i++){ // length = strlen(argv[i]); //in print_letters turning a // printf(" Length is %d \n", length); print_letters(argv[i]); //does "i" arrive as "char arg" } } int print_letters(char arg[]) { int i = 0; for(i = 0; arg[i] != '\0'; i++) { char ch = arg[i]; } //if(can_print_it(ch) { //printf(" '%c' ==%d ", ch, ch); return isalpha (ch) || isblank(ch) ; printf("'%c' == %d ", ch, ch); printf("\n"); return 0; } // } //int can_print_it(char ch) // { // return isalpha(ch) || isblank(ch); // } int main(int argc, char *argv[]) { print_arguments(argc, argv); return 0; }