I'm currently writing a program that can track each word appear in a paragraph.
For example, if a paragraph is "I have two dogs and two cats......"
my program output should be: 'I'appeared 1 time, 'have' appeared 1 time, 'two' appear 2 times ....so on

So far, I have two array. strList store each word and strCount track how many time have the word appears
I got to the point where I can split the each sentence into a word. But when I try to use strcmp to compare string, then it crash.
I don't understand why it crash. How come when I use strcmp, the program crash? Thank you so much

here is my code:

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


int main()
{
    char string1[] = "Four gunmen attempted to attack the Indian consulate in Afghanistan's western Herat province early Friday,a spokesman for the governor's office said.A member of the Indian Tibetan Border Police,who was guarding the consulate in Herat City at the time,fired at the assailants as they attempted to barge into the building,said Deepak Kumar,a spokesman for the ITBP in New Delhi.The attackers were armed with machine guns and grenades,he said.A car bomb also exploded near the scene but caused no damage.The attackers were eventually surrounded by Afghan security forces as they approached the consulate,according to Herat governor's spokesman Abdul Rauf Ahmadi.Two of the gunmen were killed,Ahmadi said.The consulate was not damaged and no one there was injured.The identity of the attackers or motive for the attack is not yet known.Meanwhile,India's external affairs spokesman,Syed Akbaruddin,said in post on Twitter that New Delhi was in touch with Kabul and that Indian Foreign Secretary Sujatha Singh was monitoring the situation.";
    char word[256];
    char *strList[155];
    int strCount[154];

    int i=0;
    int j=0;
    int k=0;

    //temp holder for for loop
    int a = 0;
    int b = 0;
    int hasIt;


    while (string1[i] != '\0') 
    {
        if (string1[i] == ' ' || string1[i] == ',' || string1[i] == '.' || string1[i] == '.') 
        {   
            word[j] = '\0';

            for(a=0; a<155; a++)
            {
                if( strcmp( strList[a],word) == 0)
                {
                    strCount[k] = strCount[k] +1;   
                }
                else
                {
                    strList[k] = word;
                    strCount[k] = 1;
                }

            }

            printf("%s %d\n", strList[k], strCount[k]);

            k++;
            b = 0;
            j = 0;
        } 
        else 
        {
            word[j++] = string1[i];
        }

        i++;
    }

    return 0;
}

strList[a] is a char* that you never set. It could be pointing anywhere in memory. It will almost certainly be pointing at memory that the operating system doesn't want this program touching, so it kills it with a segFault.

I see. Another question that I run while I'm fixing the code. How come the previous string that store in strList has been override by the current one. I did increment k, while I'm storing the string.

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


int main()
{
    char string1[] = "Four gunmen attempted to attack the Indian consulate in Afghanistan's western Herat province early Friday,a spokesman for the governor's office said.A member of the Indian Tibetan Border Police,who was guarding the consulate in Herat City at the time,fired at the assailants as they attempted to barge into the building,said Deepak Kumar,a spokesman for the ITBP in New Delhi.The attackers were armed with machine guns and grenades,he said.A car bomb also exploded near the scene but caused no damage.The attackers were eventually surrounded by Afghan security forces as they approached the consulate,according to Herat governor's spokesman Abdul Rauf Ahmadi.Two of the gunmen were killed,Ahmadi said.The consulate was not damaged and no one there was injured.The identity of the attackers or motive for the attack is not yet known.Meanwhile,India's external affairs spokesman,Syed Akbaruddin,said in post on Twitter that New Delhi was in touch with Kabul and that Indian Foreign Secretary Sujatha Singh was monitoring the situation.";
    char word[256];
    char *strList[173];
    int strCount[154];

    int i=0;
    int j=0;
    int k=0;

    while (string1[i] != '\0') 
    {
        if (string1[i] == ' ' || string1[i] == ',' || string1[i] == '.') 
        {   
            word[j] = '\0';

            strList[k] = word;

            k++;
            j = 0;
        } 
        else 
        {
            word[j++] = string1[i];
        }
        i++;

    }

    for(a=0; a<173; a++)
    {
        printf("%s\n", strList[a]);
    }


    return 0;
}

If you want to copy some char values from one char array to another, you're going to need strcpy, or even better one of the many much safer strcpy alternatives.

Just using = won't copy any char values. You need to go back and read up on what a char array is, how to allocate memory for them, and how to copy them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.