Thread: Code almost works, but not exactly, need a bit of help

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    3

    Question Code almost works, but not exactly, need a bit of help

    Just trying to write some code for personal practice, not homework or anything of that sort.


    Some weird happens: removing
    Code:
    char *wordArray1[10] = {0};
    line in the full code below causes the program to work fine until it prints the first value of 3 to indicate the word olly has shown up 3 times then the program stops and exits the console. Leaving the above code alone results in the program doing what I expected, which is to count number of times each word in the string shows up in said string. Why is that? wordArray1 is a useless variable that I wanted to use but decided it was not needed.

    I am using CodeLite/GCC compiler, not sure if that matters any to my question above and also any tips to improve full code would be appreciated. Thank you


    Code:
    // Word Count: Given a phrase, count the occurrences of each word in that phrase.
    // I am assuming case insensitivity.
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    {
        char str[] = "olly olly olly in come free";
        char *token = strtok(str, " "); // get first token
        char *wordArray[10] = {0};
        char *wordArray1[10] = {0};
        int i = 0;
        int wordCount = 0;
    
    
        // convert phrase to lowercase
        for(int j=0; j < strlen(str); j++)
        {
            str[j] = tolower(str[j]);
        }
    
    
        // walk through other tokens
        while( token != NULL )
        {
            wordCount++;
            wordArray[i++] = token;
            token = strtok(NULL, " ");
        }
    
    
        // print the words as is
        for (i = 0; i <  wordCount; i++)
        {
            printf("%s ", wordArray[i]);
        }
    
    
        printf("\n\n");
    
    
        int countArray[] = {0}; // store the entire list of word frequency counts
        for (i = 0; i <  sizeof(wordArray) / sizeof(wordArray[0]); i++)
        {
    
    
            if(wordArray[i] != NULL) // check if current word has been processed or not (processed == NULL'd)
            {
                countArray[i] = 1; // New word detected
    
    
                for(int j = i + 1; j < sizeof(wordArray) / sizeof(wordArray[0]); j++) // loop through the rest of the words
                {
                    if( (wordArray[j] != NULL) && (strcmp(wordArray[j], wordArray[i]) == 0) )
                    {
                        // found a duplicate word,
                        // count for that word increments by 1.
                        countArray[i] += 1;
    
    
                        // word at index j already processed, so set its value to NULL
                        wordArray[j] = NULL;
                    }
                }
            }
            else
            {
                // NULL indicates duplicate, set count to 0 for consistency
                countArray[i] = 0;
            }
        }
    
        for (int k = 0; k < wordCount; k++)
        {
            if(wordArray[k] != NULL)
            {
                printf("%s: ", wordArray[k]);
            }
    
    
            if(countArray[k] != 0)
            {
                printf("%d\n", countArray[k]);
            }
        }
        printf("\n");
    
    
        return 0;
    }
    Last edited by mc0134; 04-30-2020 at 10:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How this code works?
    By Thony Espinoza in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2016, 11:21 AM
  2. why this code Works.
    By jafar in forum C Programming
    Replies: 4
    Last Post: 05-01-2014, 03:53 PM
  3. How come my code works? Seriously. :)
    By assiduus in forum C Programming
    Replies: 15
    Last Post: 02-10-2011, 12:54 PM
  4. code only works with 3 digits
    By bejiz in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2005, 05:42 AM
  5. How Do u See if a code works
    By Nos in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2005, 01:34 PM

Tags for this Thread