I made this simple program:
#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
while (tolower(again) != 'n');
to
while (again != 'n' || again != 'N');
, the code doesn't run properly. I thought those 2 things should behave the same way?
Quote from: andre104 on July 22, 2009, 07:04:18 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.
Quote from: andre104 on July 22, 2009, 07:04:18 AM
2. When I change while (tolower(again) != 'n');
to while (again != 'n' || again != 'N');
, the code doesn't run properly. I thought those 2 things should behave the same way?
Try
while (again != 'n' && again != 'N');
. I think you will continue if you press a key which is not 'n' and not 'N'. ;)
Quote from: AlexN on July 22, 2009, 08:24:27 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:
for(i=0; str[i] != '\0' && str[i] != '\n'; i++)
now it works well
thank you :)
Quote from: AlexN on July 22, 2009, 08:24:27 AM
Try 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
#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;
}