Below is the output of before and after update, and it looks ok to me. What/Where is the issue. In my local run it works fine. I have pasted my code from CodeBlocks.
one fish two fish red fish blue fish
Word to update: one
Before Update
unique_words: 0
After Update
unique_words: 1
0: 1, one
============
Word to update: fish
Before Update
unique_words: 1
0: 1, one
After Update
unique_words: 2
0: 1, one
1: 1, fish
============
Word to update: two
Before Update
unique_words: 2
0: 1, one
1: 1, fish
After Update
unique_words: 3
0: 1, one
1: 1, fish
2: 1, two
============
Word to update: fish
Before Update
unique_words: 3
0: 1, one
1: 1, fish
2: 1, two
After Update
unique_words: 3
0: 1, one
1: 2, fish
2: 1, two
============
Word to update: red
Before Update
unique_words: 3
0: 1, one
1: 2, fish
2: 1, two
After Update
unique_words: 4
0: 1, one
1: 2, fish
2: 1, two
3: 1, red
============
Word to update: fish
Before Update
unique_words: 4
0: 1, one
1: 2, fish
2: 1, two
3: 1, red
After Update
unique_words: 4
0: 1, one
1: 3, fish
2: 1, two
3: 1, red
============
Word to update: blue
Before Update
unique_words: 4
0: 1, one
1: 3, fish
2: 1, two
3: 1, red
After Update
unique_words: 5
0: 1, one
1: 3, fish
2: 1, two
3: 1, red
4: 1, blue
============
Word to update: fish
Before Update
unique_words: 5
0: 1, one
1: 3, fish
2: 1, two
3: 1, red
4: 1, blue
After Update
unique_words: 5
0: 1, one
1: 4, fish
2: 1, two
3: 1, red
4: 1, blue
============
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_WORDS 20 // at most MAX_WORDS can be found in the test input string
#define MAX_WORD_LENGTH 50 // no individual word can exceed this length
// results structure
typedef struct word_count_word
{
char text[MAX_WORD_LENGTH +
1]; // allow for the string to be null-terminated
int count;
} word_count_word_t;
#define EXCESSIVE_LENGTH_WORD -1
#define EXCESSIVE_NUMBER_OF_WORDS -2
int unique_words = 0;
word_count_word_t words[MAX_WORDS];
int count_words(const char *sentence, word_count_word_t *words);
void add_word(char *word, word_count_word_t *words);
void print_words(word_count_word_t *words);
int count_words(const char *sentence, word_count_word_t *words)
{
if (!sentence)
return 0;
char *ptr1 = (char *)malloc(strlen(sentence)+1);
char *ptr2 = ptr1;
memcpy(ptr1,sentence,strlen(sentence)+1);
while (*ptr1)
{
*ptr1 = tolower(*ptr1);
if (*ptr1 == '\'' && (char)*(ptr1 + 1) != 's' && !isalpha((char)*(ptr1 - 1)) )
{
*ptr1 = ',';
}
printf("%c", (char)*ptr1);
ptr1++;
}
printf("\n%s\n", sentence);
const char delimiters[] = " @$%^&!.?:,\"\t\n\r\0";
for (char *word = strtok(ptr2, delimiters); word != NULL; word = strtok(NULL, delimiters))
{
if (strlen(word) > MAX_WORD_LENGTH)
{
return EXCESSIVE_LENGTH_WORD;
}
if (unique_words > MAX_WORDS)
{
return EXCESSIVE_NUMBER_OF_WORDS;
}
printf("Word to update: %s\n", word);
printf("Before Update\n");
printf("unique_words: %d\n", unique_words);
print_words(words);
add_word(word, words);
printf("After Update\n");
printf("unique_words: %d\n", unique_words);
print_words(words);
printf("============\n");
}
return unique_words;
}
void add_word(char *word, word_count_word_t *words)
{
for (int i=0; i<unique_words; i++)
{
if (strcmp(word, words[i].text) == 0)
{
words[i].count++;
return;
}
}
strcpy(words[unique_words].text, word);
words[unique_words].count = 1;
// unique_words < MAX_WORDS ? unique_words++ : unique_words;
unique_words++;
return;
}
void print_words(word_count_word_t *words)
{
for (int i=0; i<unique_words; i++)
{
printf("%d: %d, %s\n", i, words[i].count, words[i].text);
}
}
int main()
{
// char phrase[] = "\"That's the password: 'PASSWORD 123'!\", cried the Special Agent.\nSo I fled.";
char phrase[] = "one fish two fish red fish blue fish";
printf("unique words: %d\n", count_words(phrase, (word_count_word_t *)&words));
print_words(words);
return 0;
}