Home > other >  Counting the number of words and spaces and checking the common word
Counting the number of words and spaces and checking the common word

Time:04-07

I'm trying to write a C program that counts the number of words and spaces and checks the common word.

This is what I have:

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

int stringCount(char *str) {
    int count = 0, i, spaces;
    
    for (i = 0; *str != '\0'; i  ) {
        if (*str == ' ') {
            spaces  ;
        }
    }
    return count;
}

bool checkWord(char *str, char *word) {
    int i = 0;
    int j = 0;
    
    while (str[i] != '\0') {
        if (str[i] == word[j]) {
            j  ;
            if (word[j] == '\0') {
                return true;
            }
        } else {
            i = i - j;
            j = 0;
        }
        i  ;
    }
    return false;
}

int main() {
    char str1[20];
    char str2[20];
    char word[20];
    int i = 0;
    int j = 0;
    int k = 0;
    int count = 0;
    int count2 = 0;
    
    printf("Enter a string: ");
    fgets(str1, 20, stdin);
    printf("Enter a string: ");
    fgets(str2, 20, stdin);
    
    count = stringCount(str1);
    count2 = stringCount(str2);

    printf("The number of characters in string 1 is %d\n", count);
    printf("The number of characters in string 2 is %d\n", count2);

    printf("The common word is: ");
    if (word[k] == '\0') {
        printf("%c", word[k]);
        k  ;
    } else {
        printf("There is no Common Word");
    }
    printf("\n");
    return 0;
}

EDIT: we're forbidden using #include <string.h>

somehow the program run

EDIT: I got stuck at stringCount() and the checkWord(). for the stringCount is need to count the string with a space but I don't know how. and for the checkWord() is need to check if there is a common word not a character.

CodePudding user response:

There are multiple issues in your code:

  • the array sizes to read the strings seem a bit short at 20.
  • you do not check for fgets() possibly failing to read a string. The program will have undefined behavior if redirected from an empty file.
  • fgets() stores the trailing newline in the array if any. You should remove it before counting. You could use a function to read the string that would not store the newline and ignore extra characters input by the user.
  • stringCount() has an infinite loop: you do not update str in the loop. You should instead test str[i] != '\0'.
  • you count the spaces but do not return it to the caller. Passing a pointer to a local variable is an idiomatic way to get multiple values from a function call.
  • checkWord does not do the job: a simple approach to find the longest common word is brute force: for each word in str1, try and compare each word in str2.

Here is a modified version:

#include <stdio.h>

/* read a string from the user */
int getString(const char *prompt, char *buf, int size) {
    int c, i = 0;

    printf("%s", prompt);
    /* read a full line of characters, stop at newline or EOF */
    while ((c = getchar()) != EOF && c != '\n') {
        if (i   1 < size) {
            buf[i  ] = (char)c;
        }
    }
    putchar('\n');
    buf[i] = '\0';
    if (c == EOF && i == 0) {
        return -1;
    } else {
        return i;
    }
}

/* count the characters and spaces in the string */
int stringCount(const char *str, int *sp) {
    int i, spaces = 0;

    for (i = 0; str[i] != '\0'; i  ) {
        if (str[i] == ' ')
            spaces  ;
    }
    if (sp != NULL)
        *sp = spaces;
    return i;
}

int findCommonWord(const char *str1, const char *str2, char *buf) {
    int i, j, k, n1, n2;
    int best_i = 0, best_len = 0;

    for (i = 0; str1[i] != '\0'; i  = n1) {
        n1 = 1;
        if (str1[i] == ' ')
            continue;
        /* compute length of word starting at str1[i] */
        while (str1[i   n1] != '\0' && str1[i   n1] != ' ') {
            n1  ;
        }
        /* if the word is longer than the best match, try and find it in str2 */
        if (n1 > best_len) {
            for (j = 0; str2[j] != '\0'; j  = n2) {
                n2 = 1;
                if (str2[j] == ' ')
                    continue;
                /* compute length of word starting at str2[j] */
                while (str2[j   n2] != '\0' && str2[j   n2] != ' ') {
                    n2  ;
                }
                if (n1 == n2) {
                    for (k = 0; k < n1; k  ) {
                        if (str1[i   k] != str2[j   k])
                            break;
                    }
                    /* if comparison succeeds we have a new best match */
                    if (k == n1) {
                        best_len = n1;
                        best_i = i;
                    }
                }
            }
        }
    }
    /* copy the longest match */
    for (k = 0; k < best_len; k  ) {
        buf[k] = str1[best_i   k];
    }
    buf[k] = '\0';
    return k;
}

int main() {
    char str1[100], str2[100], word[100];
    int count1, spaces1, count2, spaces2;

    if (getString("Enter a string: ", str1, sizeof str1) < 0
    ||  getString("Enter a string: ", str2, sizeof str2) < 0)
        return 1;

    count1 = stringCount(str1, &spaces1);
    count2 = stringCount(str2, &spaces2);
    printf("string 1: %d characters, %d spaces\n", count1, spaces1);
    printf("string 2: %d characters, %d spaces\n", count2, spaces2);

    if (findCommonWord(str1, str2, word)) {
        printf("The longest common word is: %s\n", word);
    } else {
        printf("There is no common word\n");
    }
    return 0;
}

Output:

Enter a string: Hello world I am Benny
Enter a string: I am the world
string 1: 22 characters, 4 spaces
string 2: 14 characters, 3 spaces
The longest common word is: world
  •  Tags:  
  • c
  • Related