mbkadoglu
mbkadoglu

Reputation: 67

Program to count the total number of vowels, consonants and words and ask for a user again for check for a letter for a string

Program to count the total number of vowels, consonants, words and ask for a user again to check how many times a word has been used in that sentence checking in a string.

At this point my code does checks vowels and consonants and after this program has to ask for a word to the user to input a word and check how many times ıts been used in that sentence.

#include <stdio.h>

void main() {
    char sentence[80];
    int i, vowels = 0, consonants = 0, special = 0;
 
    printf("Enter a sentence \n");
    gets(sentence);
    for (i = 0; sentence[i] != '\0'; i++) {
        if ((sentence[i] == 'a' || sentence[i] == 'e' ||
             sentence[i] == 'i' || sentence[i] == 'o' ||
             sentence[i] == 'u') ||
            (sentence[i] == 'A' || sentence[i] == 'E' ||
             sentence[i] == 'I' || sentence[i] == 'O' ||
             sentence[i] == 'U')) {
            vowels = vowels + 1;
        } else {
            consonants = consonants + 1;
        }
        if (sentence[i] == '\t' ||sentence[i] == '\0' || sentence[i] == ' ') {
            special = special + 1;
        }
    }
    consonants = consonants - special;
    printf("No. of vowels in %s = %d\n", sentence, vowels);
    printf("No. of consonants in %s = %d\n", sentence, consonants);
}

Upvotes: 2

Views: 911

Answers (1)

chqrlie
chqrlie

Reputation: 144951

Here are some problems in your code:

  • void main() is invalid: you should write int main()

  • gets(sentence); is risky as input longer than 79 bytes will cause a buffer overflow. This function is a security flaw and has been removed from current versions of the C Standard. You should use fgets() instead and check the return value.

  • you only test for TAB and SPACE as special characters, so any punctuation will be counted as consonant letters.

  • you do not count the number of words

  • you only implement the first part of the assignment.

Here is a modified version using ancillary functions:

#include <stdio.h>
#include <string.h>

int is_letter(char c) {
    // Assuming ASCII
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

int is_vowel(char c) {
    return c != '\0' && strchr("aeiouAEIOU", c) != NULL;
}

int main() {
    char sentence[80];
 
    printf("Enter a sentence \n");
    if (fgets(sentence, sizeof sentence, stdin)) {
        int i, vowels = 0, consonants = 0, words = 0;
        char c, lastc = 0;

        for (i = 0; sentence[i] != '\0'; i++) {
            c = sentence[i];
            if (is_letter(c)) {
                if (is_vowel(c)) {
                    vowels = vowels + 1;
                } else {
                    consonants = consonants + 1;
                }
                if (!is_letter(last)) {
                    words = words + 1;
                }
            }
            last = c;
        }
        printf("No. of vowels     = %d\n", vowels);
        printf("No. of consonants = %d\n", consonants);
        printf("No. of words      = %d\n", words);

        // implement the rest of the assignment...
    }
    return 0;
}

Upvotes: 1

Related Questions