#9 | Pointer | Write a program to count vowels and consonants in string using pointer

 



YouTube Video :- 


Question :- 

Write a program to count vowels and consonants in string using pointer

Code :-

// Write a program to count vowels and consonants in string using pointer
// vowels = a,e,i,o,u
// consonants = reamining Alphabets from vowels

#include<stdio.h>

void main()
{
    // declare some variables
    char str[50];
    char *s;
    int vowels=0,conso=0;

    // Collect string from user

    printf("Enter Any string :- ");
    scanf("%s",str);

    // assign memory address
    // charcter pointer has value = str[0]
    // str's first element that is 0th index's memory address
    // is given to the character pointer
   
    s = str;

    // count the vowels and consonants

    while (*s != '\0')
    {
        /* code */
        if (*s == 'a' || *s == 'e' || *s == 'i' || *s == 'o' || *s == 'u' ||
            *s == 'A' || *s == 'E' || *s == 'I' || *s == 'O' || *s == 'U' )
        {
            vowels++;
        }
        else
        {
            conso++;
        }
        s++;
    }

    // print the number of vowels and consonnants

    printf("\nNumber of Vowels = %d",vowels);
    printf("\nNumber of Consonants = %d",conso);
   
}

Description :-


  • - First, We have to take one character array to store the String 
  • - Then we have to declare one character pointer that can store the address of our string
  • - After, taking a string from user, we have to assign the memory address of our string to our chracter pointer
  • - Also we have to Declare two integer variables that will calculate the length of a string
  • - Now our character pointer has the memory address of first element of string,
  • - That is s = str[0]
  • - As in our last Program (Practical 8) we have to run one while loop to get character from a string
  • - In while loop, we have give if else statements.
  • - Here the condition is to check the vowels character from string
  • - That characters are [a,e,i,o,u and A,E,I,O,U]
  • - If the condition is true, Than we have to increment the vowel
  • - else we have to increment the consonants by one
  • - To run the while loop to next character, we have to increment the character pointer by one
  • - At the End of the while loop we have to print the length of Vowels and consonants.
  • ! Thank You For Visiting !

    Comments

    Popular posts from this blog

    #6 | Pointers | Write a Program to store n Elements in an array and print them using Pointers

    #5 | Pointers | Find Maximum Number Using Pointers | #comptech #prathamrathod