Thread: Trying to count vowels... in every word of a string.

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    21

    Trying to count vowels... in every word of a string.

    I have been working on this for a long time with no success,
    so yea what am I doing wrong here.
    I'm trying analyze every word of a string, that means vowels, numbers, signs, and constants.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
    int i, length=0, count=0,vowel=0,constan=0,num=0,sign=0,h=0;
    
    char hi[800];
    char hey[800];
    fgets (hi,800,stdin);
    
    printf("%s", hi);
    
    length= strlen(hi);
    
    printf("\nlength = %d\n",length);
    
    int j = 0;
    for (i = 0; i <= length; i++)
    {
        if (hi[i] == ' ')
            {
            count++;
             for (h = 0; hey[h] == '\0'; ++h)
             {
                 if (hey[h] == 'a' || hey[h] == 'e'||hey[h] == 'i'||hey[h] == 'o'||hey[h] == 'u')
                        {++vowel;}
                    if(hey[h]>'0'&&hey[h]<'9')
                        {++num;}
                    if(hey[h] == '.' || hey[h] == '!' || hey[h] == ','||hey[h] == '?'||hey[h] == '('||hey[h] == ')'||hey[h] == '‘'||hey[h] == '“')
                        {++sign;}
                    else    
                        {++constan;}
             }
             hey[j] = '\0';
                printf("\n%s\n", hey);
                printf("\nVowels: %d",vowel);
                printf("\nConsonants: %d",constan);
                printf("\nNumbers .-.:%d",num);
                printf("\nSigns: %d",sign);
                printf("\n");
             j = 0;
             vowel=0;
             constan=0;
             num=0;
             sign=0;
           }
        else
            {          
            hey[j++] = hi[i];
            }        
    }
             ++count;
                printf("\nNumber of words :%d",count);
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to format your code properly, especially concerning indentation, e.g.,
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int i, length = 0, count = 0, vowel = 0, constan = 0, num = 0, sign = 0, h = 0;
    
        char hi[800];
        char hey[800];
        fgets (hi, 800, stdin);
    
        printf("%s", hi);
    
        length = strlen(hi);
    
        printf("\nlength = %d\n", length);
    
        int j = 0;
        for (i = 0; i <= length; i++)
        {
            if (hi[i] == ' ')
            {
                count++;
                for (h = 0; hey[h] == '\0'; ++h)
                {
                    if (hey[h] == 'a' || hey[h] == 'e'|| hey[h] == 'i'|| hey[h] == 'o'|| hey[h] == 'u')
                    {
                        ++vowel;
                    }
                    if (hey[h] > '0' && hey[h] < '9')
                    {
                        ++num;
                    }
                    if (hey[h] == '.' || hey[h] == '!' || hey[h] == ',' || hey[h] == '?' || hey[h] == '(' || hey[h] == ')' || hey[h] == '‘' || hey[h] == '“')
                    {
                        ++sign;
                    }
                    else
                    {
                        ++constan;
                    }
                }
                hey[j] = '\0';
                printf("\n%s\n", hey);
                printf("\nVowels: %d", vowel);
                printf("\nConsonants: %d", constan);
                printf("\nNumbers .-.:%d", num);
                printf("\nSigns: %d", sign);
                printf("\n");
                j = 0;
                vowel = 0;
                constan = 0;
                num = 0;
                sign = 0;
            }
            else
            {
                hey[j++] = hi[i];
            }
        }
        ++count;
        printf("\nNumber of words :%d", count);
    }
    Now, hi and hey are terrible variable names. Your variable names should be descriptive. I might name them line and word respectively.

    You have a bug here: hey[h] == '\0'. You probably wanted to write: hey[h] != '\0'.

    You don't seem to have considered what happens to the last word if the line doesn't end in a ' '. In fact, the line is likely to end with a '\n'.

    Personally, I think your approach of copying the current word and then analysing the word when encountering whitespace is a little confusing. It might be clearer to write an analyse_word function that takes a pointer to the current point in the string, and then immediately starts all that analysis of what's in the word, stopping when either whitespace or the end of the string is encountered. analyse_word then prints the analysis and returns a pointer either to one past the whitespace character, or a null pointer if the end of the string has been reached. Your loop in main can then keep calling analyse_word, updating the pointer to the current point in the string, until analyse_word returns a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A development process

    Rather than some bloated "big ball of mud" main, why don't you start with something simple.
    Code:
    int countVowels ( const char *word ) {
      // some code here
    }
    
    int main ( ) {
      printf("Test1=%d\n", countVowels("hello"));
      printf("Test2=%d\n", countVowels("world"));
    }
    When you've done that, go on to implement AND TEST
    - count consonants
    - count digits
    - count punctuation

    Then go on to
    - reading a whole line
    - splitting the line into words
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    21
    Thank you guys. I mean this is my homework and I wont get a Note so in that way its not important.
    But stuff like this should be in my exam, so thats why I really want to know how to do it. And my school is going thorugh stuff very fast and the scripts we get online arent really helpfull.

    But yea this basicly what I need to do:
    https://i.imgur.com/FHvL0vy.png
    https://i.imgur.com/CcA2230.png

    As for the struct I will try to do that later, but for now Im just using
    void wordanalyze(char *strWord);

    First of Idk how to call the function in main, like wordanalyze(&hi[800]);
    I mean I tried that and no errors but it dosn't work.

    And I could put most of my code in the void wordanalyze(char *strWord)
    like this

    Code:
    void wordanalyze (char* strWord)
    {
        int i, length = 0, count = 0, vowel = 0, constan = 0, num = 0, sign = 0, h = 0;
        int j = 0;
       char hey[800];
       length = strlen(strWord);
         printf("\nlength = %d\n", length);
       for (i = 0; i <= length; i++)
        {
            if (strWord[i] == ' '|| strWord[i]=='\n')
            {
                count++;
                for (h = 0; hey[h] != '\0'; ++h)
                {
                    if (hey[h] == 'a' || hey[h] == 'e'|| hey[h] == 'i'|| hey[h] == 'o'|| hey[h] == 'u')
                    {
                        ++vowel;
                    }
                    if (hey[h] > '0' && hey[h] < '9')
                    {
                        ++num;
                    }
                    if (hey[h] == '.' || hey[h] == '!' || hey[h] == ',' || hey[h] == '?' || hey[h] == '(' || hey[h] == ')' || hey[h] == '‘' || hey[h] == '“')
                    {
                        ++sign;
                    }
                    else
                    {
                        ++constan;
                    }
                }
                hey[j] = '\0';
                printf("\n%s\n", hey);
                printf("\nVowels: %d", vowel);
                printf("\nConsonants: %d", constan);
                printf("\nNumbers .-.:%d", num);
                printf("\nSigns: %d", sign);
                printf("\n");
                j = 0;
                vowel = 0;
                constan = 0;
                num = 0;
                sign = 0;
            }
            else
            {
                hey[j++] = strWord[i];
            }
        }
        ++count;
        printf("\nNumber of words :%d\n", count);    
    }
    But I think the wordanalyze shouldn't print the results as well.
    And even the code I first posted in this thread when I make the small corrections that laserlight said the code isnt counting properly.

    So I would like to know here how I can call the wordanalyze function from main with the string
    and Im not really sure how to make the wordanalyze function just analyze and leave the printf to the main.
    I have been working on this for a lot of hours but yea...

    Btw this is what it should look like
    https://i.imgur.com/4oPhxuY.png

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's good. I was going to suggest a structure like that precisely as a next step.

    Unfortunately, I think your instructor does want you to copy each word into a separate array. That way, you would call it as:
    Code:
    wordanalyze(strWord, wd);
    This is unfortunate because it means extra work. If you could use this prototype instead:
    Code:
    char *wordanalyze(char *strWord, struct word *wd);
    Then you could have a more efficient (and easier to implement) solution by having wordanalyze return a pointer to the substring after the current word, or a null pointer if strWord turns out to be an empty string. This way, you could write your loop such that it is controlled by the call to wordanalyze (since wordanalyze would do the parsing for the current word), rather than having to write your loop such that it does the parsing of the string for words.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Count word in a string.
    By Alexie in forum C Programming
    Replies: 27
    Last Post: 01-23-2013, 08:14 AM
  2. cant understand this , count vowels
    By y0us3f in forum C Programming
    Replies: 2
    Last Post: 10-24-2011, 06:56 PM
  3. Replies: 1
    Last Post: 03-06-2005, 10:12 AM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. Trying to count vowels in a string assignment
    By NCCMelissa in forum C Programming
    Replies: 8
    Last Post: 04-16-2002, 12:25 AM

Tags for this Thread