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

In this C Programming example, we will implement the program to count vowels and consonants in a string using a pointer and print the output on the console.

1. Count vowels and consonants in a String

To find the number of consonants and vowels in a string we iterate through all the characters of the input string using a pointer inside a while loop. In each iteration, we check whether the character is a vowel or not. If it is not a vowel then we increment the count for the coonsonants.

Example

Input String: 
HelloWorld

Output String:
Number of the Vowels: 3
Number of the Consonants: 7

Helpful topics to understand this program better are-


2. C program to count vowels and consonants in a string using pointer

Let’s discuss the execution(kind of pseudocode) for the program to count vowels and consonants in a string using a pointer in C.

  1. Initially, the program will prompt the user to enter the string. String is read and stored using predefined function fgets(s, 100, stdin). Then we call the function countVowelOrConsonant().
  2. In void countVowelOrConsonant() a pointer is assigned to the char s[100] and a while loop while(*ptr!='\0') is used where the pointer checks whether the first character of the string is not null.
  3. After the string is evaluated, the pointer *ptr pointing at the character is checked for a vowel or a consonant using if(ptr=='A' ||ptr=='E' ||ptr=='I' ||ptr=='O' ||ptr=='U' ||ptr=='a' ||ptr=='e' ||ptr=='i' ||ptr=='o' ||ptr=='u').
  4. If char is a vowel then increment the vowelCount++ or else consonantCount++. In the end, increment the pointer ptr++ to evaluate the next character.
  5. The process continues until the loop reaches the null value \0 and then we print the output on the console.

Let us now implement the above execution of the program to count vowels and consonants in a string using a pointer in C.

#include <stdio.h>
char s[100];

int vowelCount = 0, consonantCount = 0;
void countVowelOrConsonant() {
  char *ptr = s;
  while (*ptr != '\0') {
    if ((*ptr >= 65 && *ptr <= 90) || (*ptr >= 97 && *ptr <= 122)) {
      if (*ptr == 'A' || *ptr == 'E' || *ptr == 'I' || *ptr == 'O' ||
          *ptr == 'U' || *ptr == 'a' || *ptr == 'e' || *ptr == 'i' ||
          *ptr == 'o' || *ptr == 'u') {
        vowelCount++;
      } else {
        consonantCount++;
      }
    }
    ptr++;
  }
}
int main() {
  printf("Enter the string: ");
  fgets(s, 100, stdin);
  countVowelOrConsonant();
  printf("Number of the Vowels in input String is: %d\n", vowelCount);
  printf("Number of the Consonants in input String is: %d", consonantCount);
  return 0;
}
Output
Enter the string: Codingeek
Number of the Vowels in input String is: 4
Number of the Consonants in input String is: 5

3. Conclusion

In this C Programming example, we have discussed how to count vowels and consonants in a string using a pointer in C and discussed different sections of the code.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x