//John T. Rine //September 10, 2011 #include #include int cntChars(char *); char *append(char, char *); int main(int argc, char **argv) { char temp; char *ptrChar; if (argc == 1) printf("Should be at least one command line argument supplied after the file name\n"); else { //only appends to first argument after filename printf("Enter a character to append to the string supplied on the command line\n"); temp = getchar(); printf("%s\n", ptrChar = append(temp, *(argv + 1))); free(ptrChar); } return(0); } int cntChars(char *inputString) { // returned value "count" does not include '\0' int count = 0; while(*(inputString + count) != '\0') ++count; return(count); } char *append(char inputChar, char *inputString) { //element 0 1 2 //count = 0 '\0' //count = 1 A '\0' //count = 2 M y '\0' int count, i; char * pChar; count = cntChars(inputString); if (!(pChar = (char *) malloc(sizeof(char) * (count + 2)))) { fprintf(stderr,"Insufficient memory"); exit(1); } else { for(i = 0; i <= (count - 1); i++) *(pChar + i) = *(inputString + i); *(pChar + count) = inputChar; if(inputChar != '\0') *(pChar + count + 1) = '\0'; return(pChar); } }