-2

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;
    }
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52

1 Answers1

0

A brute force method. Time complexity might be O(N^2). Break into about four steps:
1. Input loop('\n' will finished one string input)
2. Split string line into words list.
3. Count every word time in list. (Brute force)
4. Print result.

#include <stdio.h>

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

#define MAXSTRLEN 1024

char **splitToList(char *a, int *rowSize) {
    char **b=NULL;
    int start=0;
    int count=0;
    for (int i = 0; i < strlen(a); ++i) {
        if (' ' == a[start]) {
            start = i;
            continue;
        }

        if (' ' == a[i]) {
            ++count;
            if (NULL == b) {
                b = (char **)malloc(sizeof(char *));
                b[count-1] = (char *)malloc(sizeof(char)*(i-start+1));
                memcpy(b[count-1], &a[start], (i-start));
                b[count-1][i-start] = '\0';
                start = i;
            } else {
                b = (char **)realloc(b, sizeof(char *)*(count));
                b[count-1] = (char *)malloc(sizeof(char)*(i-start+1));
                memcpy(b[count-1], &a[start], (i-start));
                b[count-1][i-start] = '\0';
                start = i;
            }
            continue;
        }

    }

    if (start <= strlen(a) && a[strlen(a)-1] != ' ') {
        ++count;
        b = (char **)realloc(b, sizeof(char *)*(count));
        b[count-1] = (char *)malloc(sizeof(char)*(strlen(a)-start+1));
        memcpy(b[count-1], &a[start], (strlen(a)-start));
        b[count-1][strlen(a)-start] = '\0';
    }

    *rowSize = count;

    return b;
}

bool equalChars(char *a, char *b) {
    if (strlen(a) != strlen(b)) {
        return false;
    }

    int i=0;
    while (i<strlen(a) && a[i] == b[i]) {
        ++i;
    }

    if (i < strlen(a)) {
        return false;
    }

    return true;
}

void countWords(char *a) {
    int *rowSize = (int *)malloc(sizeof(int));

    // 1. split to word list
    char **b = splitToList(a, rowSize);

    // 2. count words times
    int count[*rowSize];
    for (int i = 0; i < *rowSize; ++i) {
        if (0 == i) {
            count[i] = 1;
            continue;
        }
        int dup = false;
        for (int j = 0; j <= i-1 ; ++j) {
            if (equalChars(b[i], b[j])) {
                count[j] = count[j] + 1;
                count[i] = 0;
                dup = true;
                break;
            }
        }

        if (!dup) {
            count[i] = 1;
        }
    }

    // 3. print words times
    for (int i = 0; i < *rowSize; ++i) {
        if (count[i] != 0) {
            printf("word: %s, times: %d\n", b[i], count[i]);
        }
    }
}

int main(void) {

    char *a = (char *)malloc(MAXSTRLEN);
    int curLen = -1;

    // input loop
    int c;
    for(;;) {
        c = fgetc(stdin);
        if(c == EOF) {
            break;
        }

        if (curLen > MAXSTRLEN - 1) {
            break;
        }

        if(c == '\n') {
            a[++curLen] = '\0';
            countWords(a);
            curLen=-1;
        } else {
            a[++curLen] = (char) c;
        }
    }

}
caisil
  • 363
  • 2
  • 14