Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7905749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:35:14+00:00 2026-06-03T10:35:14+00:00

Ok, so I’m almost finished with this program for my final project and I

  • 0

Ok, so I’m almost finished with this program for my final project and I am receiving a segmentation fault… The program will do everything correctly, and it will print to the screen everything however it does not get out of the printWordLength() function. prints segmentation fault right at the end, I’m certain this is a simple fix but my brain is crashing at this moment in time. (Scroll down to very bottom for the culprit print function.

If you just want to use my code, feel free.

Purpose: This program holds a doubly linked list that will read a file that is entered as a command line argument, read each line from file, tokenize each word from line and for each word will place it into a Word Length structure depending on its length and then will place it into a word_count structure dependent on the word’s string and count each word’s occurrence in a file.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define DELIM " ,.+-=!?:;\t"
#define MAXLINE 25000

typedef struct word_count
{
    char * word;
    int count;
    struct word_count *next;
    struct word_count *prev;
} WORD;

typedef struct word_length_count
{
    int length;
    int count;
    WORD * words;
    struct word_length_count *next;
    struct word_length_count *prev;
} WLENGTH;

int splitIntoWords(char line[]);
void processLength(char * word);
void processWord(char * word, WORD * wordCount);
void printWordLength();
WLENGTH * createWordLength(char *word);
WORD * createWordCount(char *word);

WLENGTH * wordLength = NULL;

int main(unsigned int argc, unsigned char *argv[]){
    FILE *fpin;
    char line[MAXLINE];
    int totalWordCount = 0;

    if((fpin = fopen(argv[1], "r")) == NULL)
    {
            printf("Can't open input file.\n");
            exit(-1);
    }

    printf("This is the words all tokenized from the input!\n");
    while(fgets(line, MAXLINE, fpin) != NULL)
    {
            line[strcspn(line, "\n")] = '\0';
            if(line[0] == '\0')
                    continue;
            totalWordCount += splitIntoWords(line);
    }
    printf("Total number of words is: %d\n", totalWordCount);
    printWordLength();
    printf("\nFINISHED!");
}

int splitIntoWords(char line[])
{
    char *word;
    int count=0;
    word = strtok(line, DELIM);
    for(;word != NULL;)
    {
            count++;
            printf("%s\n", word);
            processLength(word);
            word = strtok(NULL, DELIM);
    }
    return count;
}

void processLength(char * word)
{
    WLENGTH *wLCounter = NULL;
    WLENGTH *wLLast = NULL;

    if(wordLength == NULL)
    {
            wordLength = createWordLength(word);
            return;
    }

    wLCounter = wordLength;

    while(wLCounter != NULL)
    {
            if(strlen(word) == wLCounter->length)
            {
                    ++wLCounter->count;
                    processWord(word, wLCounter->words);
                    return;
            }
            wLLast = wLCounter;
            wLCounter = wLCounter->next;
    }
    wLLast->next = createWordLength(word);
}

void processWord(char * word, WORD * wordCount){
    WORD * wCounter = NULL;
    WORD * wLast = NULL;

    if(wordCount == NULL)
    {
            wordCount = createWordCount(word);
            return;
    }
    wCounter = wordCount;
    while(wCounter != NULL)
    {
            if(strcmp(word, wCounter->word) == 0)
            {
                    ++wCounter->count;
                    return;
            }
            wLast = wCounter;
            wCounter = wCounter->next;
    }
    wLast->next = createWordCount(word);
}

WLENGTH * createWordLength(char *word)
{
    WLENGTH *wLCounter = NULL;
    wLCounter = (WLENGTH*)malloc(sizeof(WLENGTH));
    //wLCounter->count = (int*)malloc(int));
    //wLCounter->length = (int*)malloc(int));
    wLCounter->words = createWordCount(word);
    wLCounter->count = 1;
    wLCounter->length = strlen(word);
    wLCounter->next = NULL;
    return wLCounter;
}

WORD * createWordCount(char *word)
{
    WORD *wCount = NULL;
    wCount = (WORD*)malloc(sizeof(WORD));
    wCount->word = (char*)malloc(strlen(word+1));
    strcpy(wCount->word, word);
    wCount->count = 1;
    wCount->next = NULL;
    return wCount;
}

void printWordLength(){
    WLENGTH * temp = wordLength;
    WORD * tempWORD = wordLength->words;
    while(temp != NULL)
    {
            tempWORD = temp->words;
            printf("\nFor Word Length: %d : There are: %d occurances!\n",  temp->length, temp->count);
            while(tempWORD != NULL)
            {
                    printf("\t%s\toccurs:%d\n", tempWORD->word, tempWORD->count);
                    tempWORD = tempWORD->next;
            }
            temp = temp->next;
    }
}

I received no segmentation fault until I added the while loop for tempWORD. But the brain fart moment I am having is I don’t know the issue. Maybe pointer problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:35:15+00:00Added an answer on June 3, 2026 at 10:35 am

    Without having made an effort to understand the code, the very last line looks like a problem – you need to check temp != NULL before assigning tempWORD = temp->words. Unless you intended to do the tempWord = temp->words assignment before moving to temp->next

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.