Counting vowels

This is a discussion on Counting vowels within the C Programming forums, part of the General Programming Boards category; [Prompt the user to enter a sentence, then hit newline. Read the sentence one character at a time, until you ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Counting vowels

    1. [Prompt the user to enter a sentence, then hit newline. Read the sentence one character at a time,
    2. until you come to the newline character. Count the total number of keystrokes entered, the number of
    3. alphabetic characters, and the number of vowels ('a', 'e', 'i', 'o', and 'u'). Output these three
    4. counts]

      End Result should look like this :
      Keystrokes: 9999
      Alpha Characters: 9999
      Vowels: 9999

      Only problem is when I insert a sentence... the CMD prompt does not continue... Any Help appreciated Please

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main(int argc, char *argv[])
    {
      
      int loop = 0; //Variable used for counting in the loop.
      int keystrokes = 0; //Counts number of keystrokes.
      int letters = 0; //Counts number of total letters.
      int vowels = 0;//Counts number of vowels.
      char sentence[9999]; //Defines the character array that will hold the input sentence. and fills it with the user's input.
      
      printf("Sentence Analyzer.\n");
      printf("Input your sentence(s), then hit enter.\n");
      scanf(" %c", &sentence); 
      
      for(loop = 0; sentence[loop] != '\n'; loop++)
      {
               if (sentence[loop] = 'a'||'e'||'i'||'u')
               {
                                  ++keystrokes;
                                  ++letters;
                                  ++vowels;
                                  }
                                  else
                                  {
                                      if (isalpha(sentence[loop]) == 1)
                                      {
                                                                  ++keystrokes;
                                                                  ++letters;
                                                                  }
                                                                  else
                                                                  {
                                                                      ++keystrokes;
                                                                      }
                                                                      }
                                                                      }
                                                                      
      
      printf("keystrokes%c\n   ", keystrokes); 
      printf("Alpha Characters %c\n", letters); 
      printf("Vowels %c", vowels);
      
      
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,370
    Code:
    scanf(" %c", &sentence);
    If you're trying to read a string, you should use "%s" here, and eliminate the '&' before "sentence" (as "sentence" alone will act like a pointer to the first element of that array).

    Be warned, however, that "scanf()" will stop reading when it hits whitespace (such as a space or a tab), so entering a sentence will be difficult. (There are ways around it, but let's not worry about that now.)

    Perhaps you were intending to enter one one character at a time using "%c"? In that case, you'd need the "scanf()" in a loop, and to include the (incrementing) index number in the second argument (i.e. "&sentence[i]").

    Code:
    if (sentence[loop] = 'a'||'e'||'i'||'u')
    This is incorrect syntax.

    First of all, you want the comparison operator ( == ), not the assignment operator ( = ).

    Secondly, you need to explicitly compare the input with each possible character, i.e.

    Code:
    if(sentence[loop] == 'a' || sentence[loop] == 'e' || ... )
    You also want to use "%d" to print out an integer. %c will print out a character.

    Code:
      printf("keystrokes%c\n   ", keystrokes); 
      printf("Alpha Characters %c\n", letters);
      printf("Vowels %c", vowels);
    There might be more, but let's start with that.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Still is not fully functioning in CMD.

    Code:
    include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main(int argc, char *argv[])
    {
      
      int loop = 0; //Variable used for counting in the loop.
      int keystrokes = 0; //Counts number of keystrokes.
      int letters = 0; //Counts number of total letters.
      int vowels = 0;//Counts number of vowels.
      char sentence[9999]; //Defines the character array that will hold the input sentence. and fills it with the user's input.
      
      printf("Sentence Analyzer.\n");
      printf("Input your sentence(s), then hit enter.\n");
      scanf(" %s", sentence); 
      
      for(loop = 0; sentence[loop] != '\n'; loop++)
      {
               if(sentence[loop] == 'a' || sentence[loop] == 'e' || sentence[loop] == 'i' || sentence[loop] == 'o' ||sentence[loop] == 'u')
               {
                                  ++keystrokes;
                                  ++letters;
                                  ++vowels;
                                  }
                                  else
                                  {
                                      if (isalpha(sentence[loop]) == 1)
                                      {
                                                                  ++keystrokes;
                                                                  ++letters;
                                                                  }
                                                                  else
                                                                  {
                                                                      ++keystrokes;
                                                                      }
                                                                      }
                                                                      }
                                                                      
      
      printf("keystrokes %d\n   ", keystrokes); 
      printf("Alpha Characters %d\n", letters); 
      printf("Vowels %d", vowels);
      
      
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    ok, that makes sense...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,370
    Hmm, it seems I didn't take all of your specs into account - my apologies. You want the newline there, but using "scanf()" to take a string will remove the newline.

    Rather than use "scanf()" to read each character from the input, you might want to try "getchar()" in a "while()" loop.

    Code:
    int i = 0;
    
    while((sentence[i] = getchar()) != '\n')
        i++

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Bingo! That did the trick... I wanted to use getchar() , but was unfamiliar with it.
    Heres the final product:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main(int argc, char *argv[])
    {
      
      int loop = 0; //Variable used for counting in the loop.
      int keystrokes = 0; //Counts number of keystrokes.
      int letters = 0; //Counts number of total letters.
      int vowels = 0;//Counts number of vowels.
      int i = 0;
      char sentence[9999]; //Defines the character array that will hold the input sentence. and fills it with the user's input.
      
      printf("Sentence Analyzer.\n");
      printf("Input your sentence(s), then hit enter.\n");
      scanf(" %s", sentence);
    
    
      while((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;
                                                                      }
                                                                      }
                                                                      }
    
    
                                                                      
      
      printf("Keystrokes %d \n", keystrokes); 
      printf("Alpha Characters %d \n", letters); 
      printf("Vowels %d \n", vowels);
      
      
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,370
    You can get rid of this line now:

    Code:
    scanf(" %s", sentence);
    Have you tested it? The results are dreadfully wrong. There are dangers to simply copy+pasting code and not understanding. The code I provided was intended to illustrate a concept, not to be shoved into the program without thought.

    Think of what's happening:

    Code:
      while((sentence[i] = getchar()) != '\n')  // "while" loop
        i++;                                       // body of "while" loop
        {                                          // by the time the program gets here, the while loop is already over and this part just gets executed once
               if(sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' ||sentence[i] == 'u')
               {
                    // ...

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    If i get rid of the Scanf... I wouldnt be able to input anything unless i place a scanf() somewhere else.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,370
    One more thing I held off mentioning until now ... work on your indentation. The code you posted is difficult to skim over.

    Here is the last code you posted (errors still intact) with proper indentation.

    See how much easier it is to follow?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
     
    int main(int argc, char *argv[])
    {
        int loop = 0; //Variable used for counting in the loop.
        int keystrokes = 0; //Counts number of keystrokes.
        int letters = 0; //Counts number of total letters.
        int vowels = 0;//Counts number of vowels.
        int i = 0;
        char sentence[9999]; //Defines the character array that will hold the input sentence. and fills it with the user's input.
    
        printf("Sentence Analyzer.\n");
        printf("Input your sentence(s), then hit enter.\n");
        scanf(" %s", sentence);
     
     
        while((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;
                }
            }
        }
    
        printf("Keystrokes %d \n", keystrokes); 
        printf("Alpha Characters %d \n", letters); 
        printf("Vowels %d \n", vowels);
    
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,370
    Quote Originally Posted by rmrjr22 View Post
    If i get rid of the Scanf... I wouldnt be able to input anything unless i place a scanf() somewhere else.
    Not true. Characters are being inputted with:

    Code:
    while((sentence[i] = getchar()) != '\n')

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Yeah, sorry about the mess... just trying to fit it all together before I make it neat.
    yes, i tried it w/o the scanf() and it reads properly...
    now im trying to input another sample sentence after the first.... but the "Press any key to continue..." comes up...
    I tried...
    system("pause")
    printf("Sample sentence #2");

    return = 0;
    {

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,370
    If you added code, please post an update!

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    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 ussers input.
      
      
      
      //scanf("%s", sentence);
    
    
      while((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;
    		  }
    	  }
      }
                                                                      
      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);
    
    
      system("pause");
    
    
      printf("Sample #2");
       // had the while loop here, but it didnt do much
      
      return 0;
    }

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    I also input a sentence but do not receive a proper output:
    This is a Sentence.
    SAMPLE OUTPUT, for EACH sentence above:
    Program#5 CS110.
    Keystrokes 1
    Alpha Characters 0
    Vowels 0
    Press any key to continue . . .

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    What's going on here?
    Code:
      while((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

Page 1 of 2 12 LastLast
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, 04: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21