//John T. Rine //September 10, 2011 #include #include int cntChars(char *); char *insert(char, char *, int); int main(int argc, char **argv) { char temp = 127, temp2; int number; char *ptrChar; int count; if (argc == 1) printf("Should be at least one command line argument supplied after the file name\n"); else { printf("Enter a character to append to the string supplied on the command line\n"); temp = getchar(); getchar(); //flush!!! printf("Enter a number representing the position in which to place the character\n"); temp2 = getchar(); getchar(); //flush!!! number = temp2 - 48; printf("%s\n", ptrChar = insert(temp, *(argv + 1), number)); 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 *insert(char inputChar, char *inputString, int position) { //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 { i = 0; while(i <= (count + 1)) { if(i < position) *(pChar + i) = *(inputString + i); else if (i == position) *(pChar + i) = inputChar; else *(pChar + i) = *(inputString + i - 1); i++; } return(pChar); } }