NO

Author Topic: Counting vowels and consonants  (Read 5352 times)

andre104

  • Guest
Counting vowels and consonants
« on: July 22, 2009, 07:04:18 AM »
I made this simple program:
Code: [Select]
#include <stdio.h>
#include <ctype.h>

int main () {

char str[100];
int vowel = 0, consonant = 0;
int i;
char again = ' ';
int ch;

do {
vowel = 0;
consonant = 0;
printf("Enter a string: ");
fgets(str,sizeof(str),stdin);

for(i=0; str[i] != '\0' ; i++){
if (str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='u' || str[i]=='o'){
vowel++;
}
else if (str[i] == ' '){
}
else {
consonant++;
}
}

printf("No of vowel: %d\n", vowel);
printf("No of consonant: %d\n", consonant);
printf("\nAgain (Y/N)? ");
scanf("%c",&again);
while ((ch = getchar()) != '\n' && ch != EOF);
} while (tolower(again) != 'n');

return 0;
}

I found two pecularities:
1. The consonant counted is always extra 1. For example when your input is "a big red car", the output will be:
Quote
No of vowels: 4
No of consonants: 7
2. When I change
Code: [Select]
while (tolower(again) != 'n'); to
Code: [Select]
while (again != 'n' || again != 'N');, the code doesn't run properly. I thought those 2 things should behave the same way?

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Counting vowels and consonants
« Reply #1 on: July 22, 2009, 08:24:27 AM »
I found two pecularities:
1. The consonant counted is always extra 1. For example when your input is "a big red car", the output will be:
No of vowels: 4
No of consonants: 7
The extra count at constant is because the string contains the finishing '\n' which is counted as constant.

2. When I change
Code: [Select]
while (tolower(again) != 'n'); to
Code: [Select]
while (again != 'n' || again != 'N');, the code doesn't run properly. I thought those 2 things should behave the same way?

Try
Code: [Select]
while (again != 'n' && again != 'N');. I think you will continue if you press a key which is not 'n' and not 'N'. ;)
best regards
 Alex ;)

andre104

  • Guest
Re: Counting vowels and consonants
« Reply #2 on: July 23, 2009, 07:43:57 AM »
The extra count at constant is because the string contains the finishing '\n' which is counted as constant.

ah I see
so I changed the code a bit:
Code: [Select]
for(i=0; str[i] != '\0' && str[i] != '\n'; i++)
now it works well
thank you :)

Try
Code: [Select]
while (again != 'n' && again != 'N');. I think you will continue if you press a key which is not 'n' and not 'N'. ;)

yeah... sometimes it's easy to confuse || and &&  :D

nicolas.sitbon

  • Guest
Re: Counting vowels and consonants
« Reply #3 on: July 23, 2009, 01:09:00 PM »
Code: [Select]
#include <stdio.h>
#include <ctype.h>
#include <locale.h>

#define BUF_LEN 99

#define STR(string)  XSTR(string)
#define XSTR(string) #string

int main (void)
{
   int restart = EOF;
   char const * old_locale = setlocale(LC_CTYPE, "");
   
   do
   {
      char str[BUF_LEN + 1];
      unsigned int nr_vowel     = 0;
      unsigned int nr_consonant = 0;

      puts("Enter a string: ");
      int ret = scanf("%" STR(BUF_LEN) "[^\n]", str);

      if(ret == 1)
      {
         for(size_t i = 0; str[i] != 0; i++)
         {
            if(isalpha((unsigned char) str[i]))
            {
               switch(str[i])
               {
               case 'a': /* fall through */
               case 'e':
               case 'i':
               case 'u':
               case 'o':
                  nr_vowel++;
                  break;
               default:
                  nr_consonant++;
               }
            }
         }
      }

      scanf("%*[^\n]"), getchar();     

      printf("No of vowel: %u\n"
             "No of consonant: %u\n"
             "\n"
             "Again (Y/N)? ", nr_vowel, nr_consonant);


      restart = getchar();

      if(restart != '\n')
      {
         scanf("%*[^\n]"), getchar();
      }
   }
   while(toupper(restart) == 'Y');

   if(old_locale != NULL)
   {
      setlocale(LC_CTYPE, old_locale);
   }

   return 0;
}