Thread: Help me please

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    8

    Question Help me please

    I wrote this program. I want to divvy up the sentences to the words and store the words in an pointer array. But I got runtime error. Please help me to solve this problem. Thank u...



    Code:
    /*******************************************************
    * 
    * \file :           divSent.c
    *
    * \details: This app divvies up the sentences to the words.
    *
    *******************************************************/
    
    /*********************INCLUDES*************************/
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    #include<ctype.h>
    
    /******************MACRO DEFINITIONS********************/
    
    #define ARRAY_SIZE  200
    #define OUT_WORD    0
    #define IN_WORD     1
    
    /******************TYPE DEFINITIONS*********************/
    
    /**************** FUNCTION PROTOTYPES ******************/
    
    /*********************VARIABLES*************************/
    char s[ARRAY_SIZE];
    /****************PRIVATE FUNCTIONS**********************/
    /******************************************************
    *
    *                  cntWord
    *
    * \brief  This function counting the words.
    * 
    * \param[in/out]  p : Array's Reference 
    * \param[in/out]  size  :  Array's Size
    *
    * \return
    *      \arg\c    wordCounter  :  Number of The Words.
    *
    * \sa
    *       main
    *
    ********************************************************/
    int cntWord(char *p, int size)
    {
        int wordCounter = 0;
        int k;
        int wordFlag = OUT_WORD;
        
        for (k = 0; k < size; ++k)
        {
              if (isspace(p[k]))
              {
                    wordFlag = OUT_WORD;
              }
              else if (wordFlag == OUT_WORD)
              {
                   wordFlag = IN_WORD;
                   wordCounter++;
              }
        }
        return wordCounter;
    }
    /********************************************************
    *
    *                 getSent
    *
    * \brief  This function getting sentences.
    *
    * \param[in/out]
    *          NONE
    *
    * \return
    *       \arg\c    strcpy(ptr, s) : Array's Reference
    *
    *
    ********************************************************/
    
    char *getSent(void)
    {
       //  char  s[ARRAY_SIZE];
         char *ptr;
         //register int sizofArray;
         
         printf("please type somethings : ");
         gets(s);
         //sizofArray = strlen(s);
         ptr = (char *)malloc(strlen(s) + 1);
         if (ptr == NULL)
         {
                 printf("cannot allocate memory!");
                 exit(EXIT_FAILURE);
         }
         return strcpy(ptr, s);
    }
    
    /********************************************************
    *
    *
    *
    *
    *
    *
    *
    ********************************************************/
    int getSize(char *p)
    {
        int k;
        
        for (k = 1; p[k] != '\0'; ++k)
        
        return k;
    }
    
    /********************************************************
    *
    *                 divSent
    *
    * \brief This function divving up the sentences.
    *
    * \param[in/out]  pd  :  Reference of Getting Array
    * \param[in/out]  pArraySize  :  Number of Words
    * \param[in/out]  size   :  Size of Getting Array
    *
    * \return 
    *      \arg\c  pArray : Reference of Pointer Array
    *
    ********************************************************/
    
    char *divSent(char *pd , int size)
    {
         char *pArray[ARRAY_SIZE];
         int i;
         int k;
         int j = 0;
         
         for (i = 0; (pd + i) != '\0'; ++i)
         {
             if (isspace(*(pd + i)))
             {
                  for (k = i; ;++k)
                  {
                      if (!isspace(*(pd + k)))
                      {
                            pArray[j] = &(*(pd + k));
                            ++j;
                            i = k;
                            continue;
                      }
                  }
             }
             pArray[j] = &(*(pd + i));
         }
         return *pArray;
    }
    
    /********************************************************
    *
    *                 dspArray
    *
    * \brief This function displaying the words by randoming.
    *
    * \param[in/out]  dp  :  Reference of The Pointer Array's Members
    * \param[in/out]  size  :  Size of The Pointer Array
    * 
    * \return
    *      NONE
    *
    ********************************************************/
    
    void dspArray(char **dp, int size)
    {
         int k;
         int a;
         
         for (k = 0; k < size; ++k)
         {
             a = rand() % size;
             printf("%s\n", dp[a]);
         }
    }
                                         
    /****************PUBLIC FUNCTIONs***********************/
    
    /********************************************************
    *                   main
    *
    * \brief  Divving Up The Sentences to The Words  
    *
    * \param
    *    NONE
    *
    * \return
    *    NONE
    *
    *********************************************************/
    
    int main()
    {
        char *p1;
        char *p2;
        int size;
        int psize;
        
        p1 = getSent();
        size = getSize(p1);
        psize = cntWord(p1, size);
        p2 = divSent(p1, size);
        dspArray(&p2, psize);
        
        
        
               
        
        getch();
        return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In getSent():
    >> gets(s);
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    Probably the source of your runtime error.

    In divSent():
    >> for (i = 0; (pd + i) != '\0'; ++i)
    The condition doesn't mean what you think it means.

    for (i = 0; *(pd+i) != '\0'; ++i)

    or better yet:
    >> for (k = 1; p[k] != '\0'; ++k)
    Stick to this syntax, p[k] is easy to write and you're actually being consistent, and consistent is good.

    Another problem is if you actually want to divide up the words, it should mean splitting up a sentence into tokens. By that I mean you take a larger string and make it into smaller strings. I don't see any code that actually does this. You might want to look up functions like strspn(), strcspn() and strtok() and see how they work. Even if you aren't allowed to use them, it should give you a better idea of how much simpler text parsing can be.

    The finite state machine is a nice idea, but you aren't really using it. And to be honest, I can only look at the rest so hard.

Popular pages Recent additions subscribe to a feed