Thread: Counting vowels

  1. #16
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Thats the body of the while loop, no?
    Ah, nvr mind. Moved ++i; to end of loop.
    reads vowels now...
    but i still am unsure on how to input more than once sentence
    Last edited by rmrjr22; 11-13-2012 at 10:52 PM. Reason: needed to add more

  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This is what you wanted.
    Code:
        while((sentence[i] = getchar()) != '\n')
        {
            if(sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' ||sentence[i] == 'u')
            {
                ++keystrokes;
                ++letters;
                ++vowels;
            }
            else
            {
                if (isalpha(sentence[i]) == 1)
                {
                    ++keystrokes;
                    ++letters;
                }
                else
                {
                    ++keystrokes;
                }
            }
    
    
            i++;
        }
    Fact - Beethoven wrote his first symphony in C

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Click_here View Post
    What's going on here?
    I addressed that in post #7. Apparently we've gotten nowhere with that.

  4. #19
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Or better yet...
    Code:
        for(i=0; ((sentence[i] = getchar()) != '\n'); i++)
        {
            if(sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' ||sentence[i] == 'u')
            {
                ++keystrokes;
                ++letters;
                ++vowels;
            }
            else
            {
                if (isalpha(sentence[i]) == 1)
                {
                    ++keystrokes;
                    ++letters;
                }
                else
                {
                    ++keystrokes;
                }
            }
        }
    Fact - Beethoven wrote his first symphony in C

  5. #20
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Or even better :P
    Code:
        for(i=0; ((sentence[i] = getchar()) != '\n'); i++)
        {
            ++keystrokes;
            
            if (isalpha(sentence[i]) == 1)
            {
                ++letters;
                
                if(sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' ||sentence[i] == 'u')
                {   
                    ++vowels;
                }
            }
        }
    Fact - Beethoven wrote his first symphony in C

  6. #21
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    So far this works great... minus being unable to input more than one sentence when the program runs

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main(int argc, char *argv[])
    {
      int keystrokes = 0;			//Number of keystrokes.
      int letters = 0;					//Number of total letters.
      int vowels = 0;				//Number of vowels.
      int i = 0;
      char sentence[9999];					//Chararacter array specified to users input.
    
    
     while((sentence[i] = getchar()) != '\n')
    	     
      {
    	  if(sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' ||sentence[i] == 'u') //stating vowels
    	  {
    		  ++keystrokes;
    		  ++letters;
    		  ++vowels;
    	  }
    	  else
    	  {
    		  if (isalpha(sentence[i]) == 1) // if character is true
    		  {
    			  ++keystrokes;
    			  ++letters;
    		  }
    		  else
    		  {
    			  ++keystrokes;
    			  i++;
    		  }
    	  }
      }
    
    
      
                                                                      
      printf("SAMPLE OUTPUT, for EACH sentence above:\n");
      printf(" Program#5 CS110.\n");
      printf("Keystrokes %d \n", keystrokes); 
      printf("Alpha Characters %d \n", letters); 
      printf("Vowels %d \n", vowels);
    
    
       
      
      return 0;
    }

  7. #22
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    Hello friend
    
    SAMPLE OUTPUT, for EACH sentence above:
     Program#5 CS110.
    
    Keystrokes 12
    Alpha Characters 5
    Vowels 4
    Works great, eh?

  8. #23
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @Click_Here: Be careful of the return value of "isalpha()" - it might be better to just make sure it's true (or not zero), instead of equal to one.

    Return value: A value different from zero (i.e., true) if indeed c is an alphabetic letter. Zero (i.e., false) otherwise.

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by Matticus View Post
    @Click_Here: Be careful of the return value of "isalpha()" - it might be better to just make sure it's true (or not zero), instead of equal to one.
    I did rush it :P

    Yes, that would be much better.

    Looking at my sketch file where I copy/paste and run code for here, I seem to have done it before closing the file (?!) - I didn't repost the code though -> I remember now: I didn't think that it was worth reposting.

    Quote Originally Posted by rmrjr22
    "So far this works great... minus being unable to input more than one sentence when the program runs"
    Your code is not working, because you are incrementing 'i' only if it's not a vowel and is not alpha. What is more annoying is that in a rare laps of judgement, I showed you where to do it in post 17
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting vowels
    By skg29 in forum C Programming
    Replies: 3
    Last Post: 04-29-2012, 01:48 AM
  2. Counting vowels in a string - invalid identifier
    By hencherz in forum C++ Programming
    Replies: 11
    Last Post: 02-25-2012, 05:38 AM
  3. Need help with counting vowels prog
    By truetrini20 in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2010, 07:44 AM
  4. Counting Vowels within a string.
    By patso in forum C Programming
    Replies: 12
    Last Post: 04-09-2008, 04:21 PM
  5. counting vowels
    By trippedwire in forum C++ Programming
    Replies: 10
    Last Post: 10-01-2004, 11:58 PM

Tags for this Thread