void test_sortStringArray(const char * str) { int len; char * * strArr = explode(str, " ", &len); sortStringArray(strArr, len); char * sortedStr = implode(strArr, len, " "); printf("Sorting \"%s\" gives \"%s\"\n", str, sortedStr); destroyStringArray(strArr, len); free(sortedStr); }
void displayTogetherSorted(char** articles, char ** nouns, char ** verbs, char ** preps, int aSize, int nSize, int vSize, int pSize) { char ** allWords; int total = 0; int currentTot = 0, i = 0; total = (aSize+nSize+vSize+pSize); allWords = (char**) malloc(total * sizeof(char *)); for(i = 0; i < aSize; i++) { allWords[currentTot] = (char*) malloc(strlen(articles[i]) * sizeof(char*)); strcpy(allWords[currentTot], articles[i]); currentTot++; } for(i = 0; i < nSize; i++) { allWords[currentTot] = (char*) malloc(strlen(nouns[i]) * sizeof(char*)); strcpy(allWords[currentTot], nouns[i]); currentTot++; } for(i = 0; i < vSize; i++) { allWords[currentTot] = (char*) malloc(strlen(verbs[i]) * sizeof(char*)); strcpy(allWords[currentTot], verbs[i]); currentTot++; } for(i = 0; i < pSize; i++) { allWords[currentTot] = (char*) malloc(strlen(preps[i]) * sizeof(char*)); strcpy(allWords[currentTot], preps[i]); currentTot++; } sortStringArray(allWords,total); printArray2D(allWords,total); }
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; }
int main() { FILE * fin = NULL; char fn[MAX], input[MAX], word[MAX]; int choice = 0; int sentSize = 5; int aSize, nSize, vSize, pSize; char ** articles = NULL; char ** nouns = NULL; char ** verbs = NULL; char ** preps = NULL; char ** sentence = NULL; srand(time(NULL)); readFileName(fn); openFile(fn, fin, &aSize, &nSize, &vSize, &pSize); articles = fillArray(fn,"article", articles, aSize, fin); nouns = fillArray(fn,"noun", nouns, aSize, fin); verbs = fillArray(fn,"verb", verbs, aSize, fin); preps = fillArray(fn,"preposition", preps, aSize, fin); displayAmounts(aSize, nSize, vSize, pSize); sortStringArray(articles, aSize); sortStringArray(nouns, nSize); sortStringArray(verbs, vSize); sortStringArray(preps, pSize); do { choice = menu(); if(choice == 1) { sentence = createSentence(articles, nouns, verbs, preps, aSize, nSize, vSize, pSize); printArray2D(sentence,sentSize); }// end choice == 1 else if(choice == 2) { displayAllWords(articles, nouns, verbs, preps, aSize, nSize, vSize, pSize); }// end choice == 2 else if(choice == 3) { whichArray(input); whatWord(word); if(strcmp(input,"ARTICLES") == 0) addWord(articles, word, &aSize); else if(strcmp(input,"NOUNS") == 0) addWord(nouns, word, &nSize); else if(strcmp(input,"VERBS") == 0) addWord(verbs, word, &vSize); else if(strcmp(input,"PREPOSITIONS") == 0) addWord(preps, word, &pSize); }// end choice == 3 else if(choice == 4) { whichArray(input); whatWord(word); if(strcmp(input,"ARTICLES") == 0) removeWord(articles, word, &aSize); else if(strcmp(input,"NOUNS") == 0) removeWord(nouns, word, &nSize); else if(strcmp(input,"VERBS") == 0) removeWord(verbs, word, &vSize); else if(strcmp(input,"PREPOSITIONS") == 0) removeWord(preps, word, &pSize); }// end choice == 4 else if(choice == 5) { displayTogetherSorted(articles, nouns, verbs, preps, aSize, nSize, vSize, pSize); }// end choice == 5 }while(choice != 6); return 0; }