int main(int argc, char ** argv) { printf("Welcome to PA03!\n"); char * buff; buff = (char *) malloc(10*sizeof(char)); strcpy(buff, "This is the start"); char * er = ", and this is the end!"; int size = 10; //Testing strcat_ex printf("buff = %s\n\n\n", strcat_ex(&buff, &size,er)); //Testing explode int length; const char * hi = "What in the F**k?"; length = strlen(hi); char * * strArr = explode(hi," ", &length); printf("strArr[1] = %s\n\n\n", strArr[1]); //Testing implode printf("Implosion: %s\n\n\n",implode(strArr,4," ")); printf("Freeing Memory...\n"); free(buff); free(strArr); printf("Memory Free!\n"); return 0; }
void test_strcat_ex(char * * dest, int * n, const char * src) { printf("dest has address %p, dest = \"%s\"\n", *dest, *dest); *dest = strcat_ex(dest, n, src); printf("After concatenation, dest has address %p, dest = \"%s\"\n", *dest, *dest); }
char * implode(char * * strArr, int len, const char * glue) { //LOCAL DECLARATIONS int ind; int arrLen; int glueLen = strlen(glue); char * dest = NULL; //EXECUTABLE STATEMENTS for (ind = 0; ind < len; ind++) { arrLen = strlen(strArr[ind]); strcat_ex(&dest,&arrLen,strArr[ind]); if (ind != (len - 1)) { strcat_ex(&dest,&glueLen,glue); // glue doesn't go at the very end } } return dest; }
int main(int argc, char * * argv) { printf("Welcome to PA03.\n" "\n" "Please make sure that the swapString(...) function works\n" "\n"); printf("Print out some memory addresses for argc, argv...\n" "to illustrate how memory is laid out.\n"); printf("&argc = %p\n", &argc); printf("&argv = %p\n", &argv); printf("argv = %p\n", argv); printf("*argv = %p\n", *argv); printf("*argv = %s\n", *argv); printf("**argv = %c\n", **argv); // Let's create our own array of strings printf("\nTesting swapString(...)\n"); const char * str1 = "one"; const char * str2 = "two"; printf("Before swap, str1 == %p (i.e., '%s'), " "str2 == %p (i.e., '%s')\n", str1, str1, str2, str2); swapString(&str1, &str2); printf("After swap, str1 == %p (i.e., '%s'), " "str2 == %p (i.e., '%s')\n", str1, str1, str2, str2); printf("\n\n"); char * dest = "Hello"; const char * src = "There"; int N = 7; int *n; n = &N; strcat_ex(&dest, n, src); printf("%s", dest); return EXIT_SUCCESS; }
/** * Takes an array of strings, and concatenates the elements into a single * string, placing 'glue' between each successive element. * * strArr: an array of strings * len: the length of the array 'strArr' * glue: string to concatenate between each element of 'strArr' * * For example: * int len; * char * * strArr = explode("100 224 147 80", " ", &len); * char * str = implode(strArr, len, ", "); * printf("(%s)\n", str); // (100, 224, 147, 80) * * Hint: use strcat_ex in a for loop. */ char * implode(char * * strArr, int len, const char * glue) { int totalLength, i; totalLength = (len - 1) * strlen(glue) + 1; for(i = 0; i < len; i++) { totalLength += strlen(*(strArr + i)); } char * concatString = malloc(totalLength * sizeof(char)); *concatString = '\0'; for(i = 0; i < len - 1; i++) { strcat_ex(&concatString, &totalLength, *(strArr + i)); strcat_ex(&concatString, &totalLength, glue); } // Append last piece strcat_ex(&concatString, &totalLength, *(strArr + i)); return concatString; }
int main(int argc, char **argv) { char * src; char * dest; char * result; int n; src="World!"; dest=NULL; result=strcat_ex(&dest, &n, src); printf("src=\"World!\";\ndest=NULL;\nstrcat_ex(&dest, &n, src);\n --> gives %s with n=%d\n",result,n); result=strcat_ex(&dest, &n, ""); printf("Then strcat_ex(&dest, &n, \"\") yields --> gives %s with n=%d\n",result,n); strcpy(dest,"abc"); result=strcat_ex(&dest, &n, "def"); printf("Then strcpy(dest,\"abc\"); strcat_ex(&dest, &n, \"def\") yields --> gives %s with n=%d\n",result,n); free(dest); int outlen; int my_index; char * glue; char * save; char **outstrings=explode("abc\tdef\fghi","\t\f",&outlen); printf("explode(\"abc\\tdef\\fghi\",\"\\t\\f\",&outlen); yields:\n"); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); } glue=""; printf("implode with glue \"%s\" gives \"%s\"\n",glue,save=implode(outstrings,outlen,glue)); free(save); printf("After sorting:\n"); sortStringArray(outstrings,outlen); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); free(outstrings[my_index]); } free(outstrings); outstrings=explode("abc\t\fghi","\t\f",&outlen); printf("explode(\"abc\\t\\fghi\",\"\\t\\f\",&outlen); yields:\n"); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); } glue="++"; printf("implode with glue \"%s\" gives \"%s\"\n",glue,save=implode(outstrings,outlen,glue)); free(save); printf("After sorting:\n"); sortStringArray(outstrings,outlen); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); free(outstrings[my_index]); } free(outstrings); outstrings=explode("abc\t\fghi","\f",&outlen); printf("explode(\"abc\\t\\fghi\",\"\\f\",&outlen); yields:\n"); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); } glue="|"; printf("implode with glue \"%s\" gives \"%s\"\n",glue,save=implode(outstrings,outlen,glue)); free(save); printf("After sorting:\n"); sortStringArray(outstrings,outlen); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); free(outstrings[my_index]); } free(outstrings); outstrings=explode("","\f",&outlen); printf("explode(\"\",\"\\f\",&outlen); yields:\n"); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); } glue="\t"; printf("implode with glue \"%s\" gives \"%s\"\n",glue,save=implode(outstrings,outlen,glue)); free(save); printf("After sorting:\n"); sortStringArray(outstrings,outlen); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); free(outstrings[my_index]); } free(outstrings); outstrings=explode("\f\f\f\f","\f",&outlen); printf("explode(\"\\f\\f\\f\\f\",\"\\f\",&outlen); yields:\n"); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); } glue=""; printf("implode with glue \"%s\" gives \"%s\"\n",glue,save=implode(outstrings,outlen,glue)); free(save); printf("After sorting:\n"); sortStringArray(outstrings,outlen); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); free(outstrings[my_index]); } free(outstrings); outstrings=explode("\f\f\f\f","",&outlen); printf("explode(\"\\f\\f\\f\\f\",\"\",&outlen); yields:\n"); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); } glue="|||"; printf("implode with glue \"%s\" gives \"%s\"\n",glue,save=implode(outstrings,outlen,glue)); free(save); printf("After sorting:\n"); sortStringArray(outstrings,outlen); for (my_index=0; my_index<outlen; my_index++) { printf("\t%d:\"%s\"\n",my_index,outstrings[my_index]); free(outstrings[my_index]); } free(outstrings); return 0; }