diff --git a/LowerHalf.c b/LowerHalf.c new file mode 100644 index 0000000000000000000000000000000000000000..dc262c78355c1dbb75c9b6ac8f7e45c2b36a815d --- /dev/null +++ b/LowerHalf.c @@ -0,0 +1,16 @@ +#include <stdio.h> // Steven's Figure 15.14 "UpCase" +#include "apue.h" // modified to lower the last half +int main() +{ int c; + + while (( c = getchar()) != EOF) + { if ( islower(c)) + c = toupper(c); + if (c >= 'M') c = tolower(c); + if (putchar(c) == EOF) + err_sys("output error in upcase\n"); + if (c == '\n') + fflush(stdout); + } + exit(0); +} diff --git a/Up.c b/Up.c new file mode 100644 index 0000000000000000000000000000000000000000..c8f842d569802718af212d63b5ab7f7cbd5496ed --- /dev/null +++ b/Up.c @@ -0,0 +1,17 @@ +#include <stdio.h> // Steven's Figure 15.14 "UpCase" +#include "apue.h" +#include <ctype.h> + +int main() +{ int c; + + while (( c = getchar()) != EOF) + { if ( islower(c)) + c = toupper(c); + if (putchar(c) == EOF) + err_sys("output error in upcase\n"); + if (c == '\n') + fflush(stdout); + } + exit(0); +} diff --git a/makeargv.c b/makeargv.c new file mode 100644 index 0000000000000000000000000000000000000000..00a446a2a754cb4ad6a88bfe0cf12bb51a6fc225 --- /dev/null +++ b/makeargv.c @@ -0,0 +1,79 @@ +// makeargv.c +// takes a null terminated string s, and a string of delimiters and returns +// an array of char * pointers, leaving argvp pointing to that array. +// each of the array locations points to a null terminated "token" found in s +// defined by the delimiter array. The number of tokens found is returned by makeargv +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +/* + * Make argv array (*argvp) for tokens in s which are separated by + * delimiters. Return -1 on error or the number of tokens otherwise. + */ +int makeargv(char * s, char * delimiters, char *** argvp) +{ + char * t; + char * snew; + int numtokens; + int i; + + /* snew is real start of string after skipping leading delimiters */ + snew = s + strspn(s, delimiters); //very clever + //printf("s = %s\n", s); + //printf("delimiters = %s\n", delimiters); + //printf("strspn = %d\n", (int) strspn(s, delimiters)); + //printf("snew = %s\n", snew); + + /* create space for a copy of snew in t */ + if ((t = calloc(strlen(snew) + 1, sizeof(char))) == NULL) //only true if no + //more memory + { + *argvp = NULL; + numtokens = -1; + } + else + { /* count the number of tokens in snew */ + strcpy(t, snew); + + if (strtok(t, delimiters) == NULL) + numtokens = 0; + else + for (numtokens = 1; strtok(NULL, delimiters) != NULL; numtokens++); // THE work is in the For Construct + //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + //start at 1 and move to the next, + // if its null stop + // else increment and move to the next + + /* create an argument array to contain ptrs to tokens */ + if ((*argvp = calloc(numtokens + 1, sizeof(char *))) == NULL) + //^Notice the dereferencing //Again only true if + //no more memory + { + free(t); + numtokens = -1; + } + else + { /* insert pointers to tokens into the array */ + if (numtokens > 0) + { + strcpy(t, snew); + **argvp = strtok(t, delimiters); //set the first token + // printf("1 = %s\n", **argvp); + + for (i = 1; i < numtokens + 1; i++) + { + *((*argvp) + i) = strtok(NULL, delimiters); + //^^^^^^^^^^^^^^^^ set the other tokens + // printf("%d = %s\n", i, *((*argvp) + i)); + } + } + else + { + **argvp = NULL; + free(t); + } + } + } + + return numtokens; +}