-1

I'm making a program that is supposed to count vowels. I made a char array and via if-statement I'm checking each element of it:

for( i=0 ; sent[i]!='\0' ; i++)
{ 
   if (sent[i] = ='A'||'a'||'E'||'e'||'I'||'i'||'O'||'o'||'u') 
        { vow++; 
        }
}

Now when I'm typing "My name is Arsal" on console, it is giving output "16 vowels" which is actually the number of all alphabetic characters along with spaces in above sentence. When I'm removing "OR(s)"

if (sent[i] = ='A' /*||'a'||'E'||'e'||'I'||'i'||'O'||'o'||'u' */) 
    { vow++; 
    }

the program is giving the correct output of "1" in above sentence.

This is the complete program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    int vow=0;
    char sent[100];

    printf("Enter sentence ;  \n");
    gets(sent);
    printf("\n");
    int i,j;            
    for(i=0 ; sent[i]!='\0';i++)
    {

        if (sent[i] == 'A'||'a'||'E' || 'e' || 'O' ||'o'||'I'||'i' ||'U' ||'u')
        {
            vow++;
        }
    }
    printf("\n No.of vowels = %d",vow);

    getch();
}   

Please tell me the reason.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • You are ORing together all the bitmaps of those char binaries into one and comparing that with sent[i]. That is not what you need. – Martin James Nov 15 '15 at 06:08
  • 1
    Are you using `= =` with a space inside in your real program? – n. m. could be an AI Nov 15 '15 at 06:12
  • 1
    Nerver use `gets`, use `fgets` – Ôrel Nov 15 '15 at 06:22
  • See [Why `gets()` is too dangerous to use?](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) for an explanation of [Ôrel](https://stackoverflow.com/users/4605105/%c3%94rel)'s succinct and accurate but inscrutable [comment](https://stackoverflow.com/questions/33716927/why-if-statement-is-not-considering-ors#comment55204536_33716927) – Jonathan Leffler Nov 15 '15 at 08:38

5 Answers5

3

The format of your "or"s are incorrect...

Instead of if ( sent[i] == 'A' || 'a' || 'e' .... etc It needs to be: if ( sent[i] == 'A' || sent[i] == 'a' || sent[i] == 'e' ... etc

TonyB
  • 927
  • 6
  • 13
1

Your expression:

sent[i] == 'A'||'a'||'E' || 'e' || 'O' ||'o'||'I'||'i' ||'U' ||'u'

will evaluate to non-zero (that is a true value) for every value of i.

What you seem to want is something like:

if (strchr("AEIOUaeiou", sent[i])) {
    ++vow;
}
ldav1s
  • 15,885
  • 2
  • 53
  • 56
  • 1
    You really ought to explain _how_ the expression is incorrect and show what the correct version would be. Teach him how to fish. Don't just give him a fish. – Nik Bougalis Nov 15 '15 at 07:18
1

Keep in mind that In C, all non-zero integers are considered as true and 0 is considered to be false.

So, here:

sent[i] == 'A'||'a'||'E' || 'e' || 'O' ||'o'||'I'||'i' ||'U' ||'u'

sent[i] == 'A' will be checked first. It is a condition that compares sent[i] with 'A' and returns 1 if they are equal and 0, if they aren't.

Now, the rest of the stuff ('a' , 'E' etc) are just positive integers (See the ASCII table). And these evaluate to true as they are non-zero.

When at least one of the operands of || are true, the entire condition becomes true (See the truth table of Logical OR).

Hence, the body of the if gets executed whatsoever.


The fix for this is given in other answers.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

You can use lowerto and a switch case

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <ctype.h>

main()
{
    int vow=0 ;

    char sent[100];

    printf("Enter sentence ;  \n");
    fgets(sent, sizeof(sent), stdin);
    printf("\n");
    int i;            
    for(i=0 ; sent[i]!='\0';i++)
    {
        switch (tolower(sent[i])) {
          case 'a':
          case 'e':
          case 'o':
          case 'i':
          case 'u':
            vow++;
        }

    }
    printf("\n No.of vowels = %d",vow);             

    getch();
}  
AntonH
  • 6,359
  • 2
  • 30
  • 40
Ôrel
  • 7,044
  • 3
  • 27
  • 46
0

This works

#include<stdio.h>
#include<conio.h>
#include<string.h>

int main(  )
{
  int vow = 0;

  char sent[100];

  printf( "Enter sentence ;  \n" );
  gets( sent );
  printf( "\n" );
  int i, j;
  char c;

  for ( i = 0; sent[i] != '\0'; i++ )
  {

    //    if (sent[i] == 'A'||'a'||'E' || 'e' || 'O' ||'o'||'I'||'i' ||'U' ||'u')

    c = toupper( sent[i] );
    if ( c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' )
    {

      vow++;
      printf( "\n%c is vowel", sent[i] );
    }

  }
  printf( "\n No.of vowels = %d", vow );

  getch(  );
}
BobRun
  • 756
  • 5
  • 12