adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C & C++ to count the number of vowels and consonants in a string Using conditional operator

Program in C & C++ to count the number of vowels and consonants in a string Using conditional operator



C
CPP
#include<stdio.h>
#include<ctype.h>
 int main()
  {
   char sptr[40],ch;
   int v_count=0,c_count=0,i;
   printf("Enter string:-");
   gets(sptr);
   i=0;
   while(sptr[i]!='\0')
     {
     if(sptr[i]>=65&&sptr[i]<=90||sptr[i]>=97&&sptr[i]<=122)
      {
      ch=toupper(sptr[i]);
        (ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') ? v_count++ : c_count++;
    }
     i++;
    }
    printf("\nNumber of vowels = %d",v_count);
    printf("\nNumber of consonants = %d",c_count);
   return 0;
  }
#include<iostream>
#include<ctype.h>
 using namespace std;
 int main()
  {
   char sptr[40],ch;
   int v_count=0,c_count=0,i;
   cout<<"Enter string:-";
   cin.getline(sptr,40);
   i=0;
   while(sptr[i]!='\0')
     {
      if(sptr[i]>=65&&sptr[i]<=90||sptr[i]>=97&&sptr[i]<=122)
    {
      ch=toupper(sptr[i]);
            (ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') ? v_count++ : c_count++;
   }
     i++;
    }
    cout<<"\nNumber of vowels = "<<v_count;
    cout<<"\nNumber of consonants = "<<c_count;
   return 0;
  }
Output:-









Another  style

#include<stdio.h>
#include<ctype.h>
 int main()
  {
   char sptr[40],ch;
   int v_count=0,c_count=0,i;
   printf("Enter string:-");
   gets(sptr);
   i=0;
   while(sptr[i]!='\0')
     {
      ch=toupper(sptr[i]);
      if(ch>=65&&ch<=90)
      {
        (ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') ? v_count++ : c_count++;
      }
     i++;
    }
    printf("\nNumber of vowels = %d",v_count);
    printf("\nNumber of consonants = %d",c_count);
   return 0;
  }


Related programs

  1. Program in C & C++ to count the number of vowels and consonants in a string Using switch case.
  2. Program in C & C++ to count the number of vowels and consonants in a string Using is else statement

Post a Comment

0 Comments