adsense

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

Program in C and C++ to count the number of vowels and consonants in a string Using function

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



C
CPP
#include<stdio.h>
#include<ctype.h>
 void Fun(char sptr[],int *v_count,int *c_count) //Call by address
  {
    int i=0;
     while(sptr[i]!='\0')
     {
      if(sptr[i]>=65&&sptr[i]<=90||sptr[i]>=97&&sptr[i]<=122)
  {
           switch(toupper(sptr[i]))
            { 
              case 'A':
              case 'E':
              case 'I':
              case 'O':
              case 'U':*v_count+=1;
                       break;
              default: *c_count+=1;
            }  
         }
       i++;
    } 
  }
 int main()
  {
    char str[40];
    int v=0,c=0;
    printf("Enter string:-");
    gets(str);
    Fun(str,&v,&c);
    printf("\nNumber of vowels = %d",v);
    printf("\nNumber of consonants = %d",c);
   return 0;
  }
#include<iostream>
#include<ctype.h>
 using namespace std;
 void Fun(char sptr[],int &v_count,int &c_count) //Call by reference 
  {
    int i=0;
    while(sptr[i]!='\0')
     {
      if(sptr[i]>=65&&sptr[i]<=90||sptr[i]>=97&&sptr[i]<=122)
    {
      switch(toupper(sptr[i]))
        {
         case 'A':
         case 'E':
         case 'I':
         case 'O':
         case 'U':v_count+=1;
                 break;
         default: c_count+=1;
          }  
      }
     i++;
    } 
  }
 int main()
  {
    char str[40];
    int v=0,c=0;
    cout<<"Enter string:-";
    cin.getline(str,40);
    Fun(str,v,c);
    cout<<"\nNumber of vowels = "<<v;
    cout<<"\nNumber of consonants = "<<c;
   return 0;
  }
Output:-









Above function can also be write in given below style



  void Fun(char *sptr,int &v_count,int &c_count)
  {
   while(*sptr!='\0')
     {
      if(*sptr>=65&&*sptr<=90||*sptr>=97&&*sptr<=122)
      {
       switch(toupper(*sptr))
        {
         case 'A':
         case 'E':
         case 'I':
         case 'O':
         case 'U':v_count++;
                 break;
         default: c_count++;
       }  
    }
     sptr++;
    } 
  }

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
  3. Program in C & C++ to count the number of vowels and consonants in a string Using conditional operator


Calculator 

Calculate vowels and consonants


Post a Comment

0 Comments