• Register
143 points
‚óŹ3

C++ Pointers

Given a string of lowercase english alphabets. The task is to count number of vowels present in a string using pointers.

  1. Initialize the string using a character array.
  2. Create a character pointer and initialize it with the first element in array of character (string).
  3. Create a while loop to count vowels.
  4. Iterate the loop till character pointer find ‘\0’ null character, and as soon as null character encounter, stop the loop.
  5. Check whether any vowel is present or not while iterating the pointer, if vowel found increment the count.
  6. Print the count.
  7. By using same process we count the vowels in the character string.
    #include <iostream>
    using namespace std;
    int main()
    {
        int v=0,c=0;
        char arr[100];
        char *ptr=arr;
        cout <<"Enter  a word:"<<endl;
        cin>>arr;
        while (*ptr!=0)
        {
            if (*ptr=='A'||*ptr=='E'||*ptr=='I'||*ptr=='O'||*ptr=='U'||*ptr=='a'||*ptr=='e'||*ptr=='i'||*ptr=='o'||*ptr=='u')
                ++v;
            else
                ++c;
            ptr++;
        }
        cout<<"Vowels: "<<v<<endl;
        cout<<"Consonants: "<<c;
    }

    Output

Enter a word: string                                                                                       
vowels : 1                                                                                         
consonants : 5