//John T. Rine //September 10, 2011 #include #include int cntChars(char *); char *prepend(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 { printf("Enter a character to prepend to the string supplied on the command line\n"); temp = getchar(); printf("%s\n", ptrChar = prepend(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 *prepend(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 { *pChar = inputChar; for(i = 1; i <= (count + 1); i++) *(pChar + i) = *(inputString + i - 1); return(pChar); } }