I need to write a program that makes the user input a string, then the program asks the user to continue to input more strings or not. After that, the program should count all the occurrences of the words in all the strings the user input. This code only counts the ocurrence of the last string, how should I change it?
#include <stdio.h>
#include <string.h>
#define MAXSTRLEN 100
int main(void)
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char p[50][100], str1[20], ptr1[50][100];
char* ptr;
char str[MAXSTRLEN], Y_or_N;
printf("Write a sentence.\n");
while (1)
{
gets(str);
printf("Continue or not? (y or n): ");
scanf("%c", &Y_or_N);
getchar();
if (Y_or_N == 'n')
break;
}
for (i = 0; str[i] != '\0'; i++)
{
if ('A' <= str[i] && str[i] <= 'Z')
str[i] = str[i] + 32;
}
for (i = 0; i < strlen(str); i++)
{
if ((str[i] == ' ') || (str[i] == ',' && str[i + 1] == ' ') || (str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0; j < strlen(str); j++)
{
if ((str[j] == ' ') || (str[j] == 44) || (str[j] == 46))
{
p[i][k] = '\0';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
k = 0;
for (i = 0; i <= space; i++)
{
for (j = 0; j <= space; j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0; i < count; i++)
{
for (j = 0; j <= space; j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
printf("%s : %d times.\n", ptr1[i], c);
c = 0;
}
}