BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Count Vowels and Consonants in a String using Pointer

Last Updated: February 24, 2019 by Chaitanya Singh | Filed Under: C Programs

In this tutorial, we will write a C program to count vowels and consonants in a given String using Pointer.

To understand this program you should know the basics of Arrays and pointers in C.

Program to count Vowels and Consonants in String using Pointer

In the following program we have declared a char array str to hold the input string which we store in the array using fgets() function. We have assigned the base address of array (address of first element) to the pointer p. We cycled through all the characters of the input string by using pointer p inside while loop and incrementing the pointer value on every iteration.

#include <stdio.h>
int main()
{
    char str[100];
    char *p;
    int  vCount=0,cCount=0;

    printf("Enter any string: ");
    fgets(str, 100, stdin);

    //assign base address of char array to pointer
    p=str;

    //'\0' signifies end of the string
    while(*p!='\0')
    {
        if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'
        		||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
            vCount++;
        else
            cCount++;
        //increase the pointer, to point next character
        p++;
    }

    printf("Number of Vowels in String: %d\n",vCount);
    printf("Number of Consonants in String: %d",cCount);
    return 0;
}

Output:
C Program to Count Vowels and Consonants in a String using Pointer

Related C examples

1. C program to print String using Pointer
2. C program to swap two numbers using Pointers
3. C program to create initialize and access pointer variable

Top Related Articles:

  1. C Program to concatenate two strings without using strcat
  2. C Program to swap first and last elements of an array
  3. C Program to Access Array Elements Using Pointer
  4. C Program to Calculate Average Using Array
  5. C Program to find greatest of three numbers

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Buno says

    September 17, 2020 at 4:23 PM

    What about if there is space in between (ex: Hello World). This program also counts the space between two strings as consonant, right? any solution for this

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap