I am in need for some assistance....I need to create a program that counts the number of words in certain program. The requirements for the program are
1) The program first reads a text from a txt file.
2)It counts how many times each word appears in the file
3) It displays the word and how many times it appears at the end...
sorry for the bad explanation. Here's an example.
It would call up a txt file such as test.txt
This is a dog.
This is a cat.
The dog loves the cat!!
In this case it needs to count like so
This=1
is=2
a=2
dog.=1
That=1
cat.=1
The=1
dog=1
loves=1
the=1
cat!!=1
Although its an incomplete program, meaning that it counts "dog" and "dog." as two different words, my professor wants it to work out like so....
Another requirement is I combine these two programs I've created before
This is a 2d array programCode:#include<stdio.h> #define SENTENCE_MAX 80 #define LIST_MAX 100 int main(){ int i,j; char ch, sentence[SENTENCE_MAX], Slist[LIST_MAX][SENTENCE_MAX]; for(i=0;i<LIST_MAX;i++){ Slist[i][0]=0; } printf("*------- Sentence List Program -------* \n"); i=0; do{ ch=getc(stdin); j=0; while((ch!=10)&&(ch!=EOF)&&(j<SENTENCE_MAX)){ sentence[j]=ch; j++; ch=getc(stdin); } sentence[j]=0; strcpy(Slist[i],sentence); i++; }while(ch!=EOF); printf("*------- Input file ---------* \n"); for(j=0;j<i;j++){ printf("%s \n",Slist[j]); } }
The other program I have to use is
This program is suppose to count how many times the same letter appears in a text file...However, its still incomplete (the ??????) parts...Code:#include<stdio.h> #define LIST_MAX 30 int main(){ int i, CHcount[LIST_MAX]; char ch, CHlist[LIST_MAX]; FILE *textin; for(i=0;i<LIST_MAX;i++){ CHcount[i]=0; CHlist[i]=0; } textin = fopen("test.txt","r"); do{ ch=fgetc(textin); for(i=0;((???????)&&(???????));i++){ if(ch==CHlist[i]) break;} if(CHcount[i]>0){ ???????; } else{ ?????? ; ??????; } }while((ch!=EOF)); for(i=0;((???????)&&(???????));i++){ printf("%c = %d \n",CHlist[i],CHcount[i]); } fclose(textin); }
I need to combine these two codes in order to create a program that counts how many times a word is repeated....I'm just having a very difficult time becuase everything is already fixed by the professor and not much freedom is given. Any help is appreciated!!!!!!!!
I would do it a little different.
I know you is required to use the two programs you've done before, but I will show you another slightly different solution to your problem, mostly because I will not do your homework. I've made a program, you can use it to look up how to do it, take snippets, or whatever you want.
I've commented the code, so I'll not say anymore about it.Code:/* This is the libraries we're using... stdio: printf getc fopen fclose string: memset strcmp strcpy */ #include <stdio.h> #include <string.h> /* Some defines used by the application */ #define FILE_NAME "test.txt" #define WORDS_AMMOUNT 100 #define WORDS_LENGTH 50 /* This is the structure, which holds the words _word_ is the array of characters that holds each string. _ammount_ holds the ammount of occurrences of the string. */ typedef struct { char word[WORDS_LENGTH]; int ammount; } WORDS; /* Fills an array of characters with NUL (\0) */ void clearString(char *string) { memset(string, '\0', sizeof(string)); } /* Initializes the list of words */ void initialize(WORDS *list, int size) { int x; for(x = 0; x < size; x++) { clearString(list[x].word); list[x].ammount = 1; } } /* Check if the specific string (needle) is in the array (haystack) - the array is a fixed size (ammount). */ int inArray(WORDS *haystack, char *needle, int ammount) { int i; for(i = 0; i < ammount; i++) if(strcmp(haystack[i].word, needle) == 0) /* We found it, return index-position */ return i; return -1; /* We didn't find anything, return not-true */ } int main() { FILE *fileHandle; WORDS wordList[WORDS_AMMOUNT]; char string[WORDS_LENGTH]; int stringCount = 0; int listCount = 0; int listIndex, i; /* Initialize list and clear string */ initialize(wordList, WORDS_AMMOUNT); clearString(string); /* Create file stream */ fileHandle = fopen(FILE_NAME, "r"); do { /* We get one character from the filestream into the string */ string[stringCount] = getc(fileHandle); /* Check if the counter is at a space or newline, ... */ if(string[stringCount] == 32 || string[stringCount] == 10) { /* ... if it is, clear that character. */ string[stringCount] = '\0'; /* Then check if the words already is in the list */ if((listIndex = inArray(wordList, string, listCount)) != -1) wordList[listIndex].ammount++; /* if it is, increase the occurrence of the word... */ else { /* else insert the word and increase the counter of the list */ strcpy(wordList[listCount].word, string); listCount++; } /* When we are finished using the string we're clearing it, and set the counter to zero */ clearString(string); stringCount = 0; } else stringCount++; } while(string[stringCount-1] != EOF); /* Close the file stream */ fclose(fileHandle); /* Print the result to the user */ for(i = 0; i < listCount; i++) { printf("WORD #%d\n", i); printf(" - Word: %s\n", wordList[i].word); printf(" - Times occured: %d\n", wordList[i].ammount); printf("\n"); } return 0; }
do u know how could i sort the words on alphabet, i have to write the same program but i hae to sort the words on alphabet. any idea how to do that
Use the qsort library function and use strcmp() as the comparison function. You'll have to use a wrapper function for strcmp() since the prototypes are different:
Instead of passing strcmp() to qsort(), you would pass your wrapper function.Code:int mycmpfunc(const void *a, const void *b) { return strcmp((const char *)a, (const char *)b); }
sudo rm -rf /
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks