-2
//schecking no of vowels in a string.

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

void printstring();

int main() {

    char sai[8];           //allows 8 charecters. 
    char a,e,j,o,u;        //I have used j as i have already used i for iteration
    fgets(sai, 8, stdin);  //going to enter my name
    char *ptr = sai;       ///setting pointer for string
    int i,t = 0;

    for(i = 0;i <= 7; i++){
        if(*(ptr+i) == a) || (*(ptr+i) == e) || (*(ptr+i) == j) || (*(ptr + i) == o) || (*(ptr + i) == u){
            t = t+1;
        }

        else {
            t = t;
        }
}
printf("%d", t);
}

OUTPUT:

The compiler generated an error:

jill.c: In function 'main':
jill.c:12:23: error: expected expression before '||' token
     if(*(ptr+i) == a) || (*(ptr+i) == e) || (*(ptr+i) == j) || (*(ptr + i) == o) || (*(ptr + i) == u){
                       ^~

I expected the number of vowels as output, but an error has occured. Where am I going wrong?

Harith
  • 4,663
  • 1
  • 5
  • 20

1 Answers1

1

Below listed points are incorrect in your code

  1. Englobe the full condition between brackets in your if condition is missing
  2. Single quotes are missing for character in your if statements. please go through here more explanation.
  3. As comment from  Ted Lyngmo, Craig Estey a,e,j,o,u are uninitialized.

if (*(ptr + i) == a) ||(*(ptr + i) == e) || (*(ptr + i) == j) || (*(ptr + i) == o) || (*(ptr + i) == u)

changed to

if((ptr[i] == 'a') || (ptr[i] == 'e') || (ptr[i] == 'j') || (ptr[i] == 'o') || (ptr[i] == 'u'))

code

//schecking no of vowels in a string.
#include <stdio.h>
#include <string.h>
void printstring();
int main()
{
    char sai[8];//allows 8 charecters. 
    char a,e,j,o,u; //I have used j as i have already used i for iteration
    fgets(sai, 8, stdin);  //going to enter my name
    char *ptr = sai; ///setting pointer for string
    int i,t = 0;
    for(i = 0;i <= 7; i++)
    {
        if((ptr[i] == 'a') || 
           (ptr[i] == 'e') || 
           (ptr[i] == 'j') || 
           (ptr[i] == 'o') || 
           (ptr[i] == 'u'))
           {
                t = t+1;
           }
        else
        {
            t = t;
        }
    }
    printf("%d", t);
  }

Link for below Output:

apple
2
user23
  • 41
  • 7