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;
}