Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * words.c
- *
- * Created on: October 6, 2013
- * Author: Chris Cirefice
- */
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include "words.h"
- int getNumUniqueWords(char text[], int size);
- int main(int argc, char* argv[]) {
- setvbuf(stdout, NULL, 4, _IONBF); // For Eclipse... stupid bug. --> does NOT affect the program, just the output to console!
- int nbr_words;
- char text[] = "Some - \"text, a stdin\". We'll have! also repeat? We'll also have a repeat!";
- int length = sizeof(text);
- nbr_words = getNumUniqueWords(text, length);
- return 0;
- }
- void free_memory(char **list, int size) {
- for (int i = 0; i < size; i ++) {
- // You can see that printing the values is fine, as long as free is not called.
- // When free is called, the program will crash if (size > strlen(list[i]))
- //printf("Wanna free value %d w/len of %d: %s\n", i, strlen(list[i]), list[i]);
- free(list[i]);
- }
- free(list);
- }
- int getNumUniqueWords(char text[], int length) {
- int numTotalWords = 0;
- char *word;
- printf("Length: %d characters\n", length);
- char totalWords[length];
- strcpy(totalWords, text);
- word = strtok(totalWords, " ,.-!?()\"0123456789");
- while (word != NULL) {
- numTotalWords ++;
- printf("%s\n", word);
- word = strtok(NULL, " ,.-!?()\"0123456789");
- }
- printf("Looks like we counted %d total words\n\n", numTotalWords);
- char *uniqueWords[numTotalWords];
- char *tempWord;
- int wordAlreadyExists = 0;
- int numUniqueWords = 0;
- char totalWordsCopy[length];
- strcpy(totalWordsCopy, text);
- for (int i = 0; i < numTotalWords; i++) {
- uniqueWords[i] = NULL;
- }
- // Tokenize until all the text is consumed.
- word = strtok(totalWordsCopy, " ,.-!?()\"0123456789");
- while (word != NULL) {
- // Look through the word list for the current token.
- for (int j = 0; j < numTotalWords; j ++) {
- // Just for clarity, no real meaning.
- tempWord = uniqueWords[j];
- // The word list is either empty or the current token is not in the list.
- if (tempWord == NULL) {
- break;
- }
- //printf("Comparing (%s) with (%s)\n", tempWord, word);
- // If the current token is the same as the current element in the word list, mark and break
- if (strcmp(tempWord, word) == 0) {
- printf("\nDuplicate: (%s)\n\n", word);
- wordAlreadyExists = 1;
- break;
- }
- }
- // Word does not exist, add it to the array.
- if (!wordAlreadyExists) {
- uniqueWords[numUniqueWords] = malloc(strlen(word));
- uniqueWords[numUniqueWords] = word;
- numUniqueWords ++;
- printf("Unique: %s\n", word);
- }
- // Reset flags and continue.
- wordAlreadyExists = 0;
- word = strtok(NULL, " ,.-!?()\"0123456789");
- }
- // Print out the array just for funsies - make sure it's working properly.
- for (int x = 0; x <numUniqueWords; x++) {
- printf("Unique list %d: %s\n", x, uniqueWords[x]);
- }
- printf("\nNumber of unique words: %d\n\n", numUniqueWords);
- // Right below is where things start to suck.
- free_memory(uniqueWords, numUniqueWords);
- return numUniqueWords;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement