This program at this stage is supposed to get text from the user and separate it into paragraphs, sentences and words. The code works fine so far on my system(ubuntu 18.04) but when i submit it to the bot that is supposed to give input to it, a stack smashin error comes up. Is there a way to make my code read input without crashing?
Edit:
Here's a test input. I think the problem is that it reads it all at once.(also there are some other options apart from ap: that i haven't made yet):
Test 3 Input: ap:At the center of the novel is the typical
Graham Greene character.[
]fw:h[
]fs:ovel[ ]fp:typical[
]owf[
]owl[
]ap:He is tired
and skeptical, basically decent yet cynical. One senses
that life has no real colors to him, in fact it bores him, yet
there is an underlying hope of some redemption, of some
sense of meaning.[
]fw:or[
]fw:is[
]
owf[
]owl[
]qt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void ap(char ***p, char ***s, char ***w, int *n_p, int *n_s, int *n_w)
{
char *temp = malloc(10001 * sizeof(char));
int i, k, j, length;
if(temp == NULL)
{
printf("Could not allocate memory");
return;
}
fgets(temp, 10001, stdin);
while(temp[0] == ' ')
for(i = 0;i < strlen(temp);i++)
temp[i] = temp[i + 1];
//paragraphs
*n_p += 1;
**p = realloc(**p, *n_p * sizeof(char *));
*(*p + *n_p - 1) = malloc((strlen(temp) + 1) * sizeof(char));
if(**p == NULL || *(*p + *n_p - 1) == NULL)
{
printf("Could not allocate memory");
return;
}
strcpy(*(*p + *n_p - 1), temp);
//sentences
while(temp[0] != '')
{
k = strcspn(temp, ".!?;");
length = strlen(temp);
temp[k] = '';
*n_s += 1;
**s = realloc(**s, *n_s * sizeof(char *));
*(*s + *n_s - 1) = malloc((strlen(temp) + 1) * sizeof(char));
if(**s == NULL || *(*s + *n_s - 1) == NULL)
{
printf("Could not allocate memory");
return;
}
strcpy(*(*s + *n_s - 1), temp);
j = 0;
for(i = k + 1;j <= length - k;i++)
{
temp[j] = temp[i];
j++;
}
while(temp[0] == ' ')
for(i = 0;i < strlen(temp);i++)
temp[i] = temp[i + 1];
k = strcspn(temp, "
");
while(temp[k + 1] == ' ')
for(i = k;i < strlen(temp);i++)
temp[i] == temp[i + 1];
if(!(strcmp(*(*s + *n_s - 1), "
")))
{
free(*(*s + *n_s - 1));
*n_s -= 1;
}
}
}
int main()
{
char **paragraphs, **sentences, **words, option[5];
int num_par = 0, num_sent = 0, num_words = 0, i;
paragraphs = malloc(sizeof(char *));
sentences = malloc(sizeof(char *));
words = malloc(sizeof(char *));
if(paragraphs == NULL || sentences == NULL || words == NULL)
{
printf("Could not allocate memory");
return 1;
}
do
{
scanf("%s", option);
if(!(strcmp(option, "ap:")))
ap(¶graphs, &sentences, &words, &num_par, &num_sent, &num_words);
}
while(strcmp(option, "qt"));
//test
for(i = 0;i < num_par;i++)
printf("%s", paragraphs[i]);
printf("------------- %d
", num_sent);
for(i = 0;i < num_sent;i++)
printf("%s
", sentences[i]);
return 0;
}
See Question&Answers more detail:
os