In this article, we will write a C program to count vowels in a string using Pointers.
The program takes a string from the user as input and prints the total number of vowels in that string.
Sample input:
Enter a string: Hello, how are you?
Output:
The number of vowels in the string are: 7
In C programming language, a pointer is used to hold the memory address of a variable. When we assign a string to a pointer variable, it by default holds the memory address of the very first character of the string.
So, if we want to move to the next character of the string, we can increment the pointer by one. Similarly, to move to the previous character of the string, we can decrement the pointer by one.
The following C program shows how you can count the vowels in a string using pointers:
// C program to count vowels in a string using pointers #include <stdio.h> int main() { char str[100], * ptr; int vowelsCount; printf("Enter a string: "); gets(str); // Pointer ptr points to first char of string ptr = str; // Runs until last char is reached while ( * ptr != '\0') { // Check if current character is a vowel(a,e,i,o,u) upper or lower case if ( * ptr == 'a' || * ptr == 'e' || * ptr == 'i' || * ptr == 'o' || * ptr == 'u' || * ptr == 'A' || * ptr == 'E' || * ptr == 'I' || * ptr == 'O' || * ptr == 'U') { vowelsCount++; } // Move pointer to next character ptr++; } // Print the count of vowels printf("The number of vowels in the string are: %d", vowelsCount); return 0; }
Output:
Enter a string: Hello, how are you? The number of vowels in the string are: 7
Code Explanation:
- The program takes the string from the user as input and stores it in the
str
array. - To get the string from the user, we used the
gets()
function instead of thescanf()
function. This is because thescanf()
function stops reading the string if it encounters a white space. - The statement
ptr = str
sets the pointer*ptr
to the memory address of the first character of the stringstr
. - The
while
loop runs until it reaches the last character of the string. In C, the last character of a string is always a null character'\0'
. - Inside the
while
loop, we have put anif
statement which checks if the current character that the pointer*ptr
is pointing to is an uppercase or lowercase vowel. If yes, the vowel count increases by one. - In each iteration of the while loop, the pointer
*ptr
increases by one i.e. moves to the next character of the string. - Finally, the count of the vowels is printed on the screen using the printf() function.
Example 2: Using a Function with Pointers to Count Vowels
We can also create a user-defined function where we can put all our logic to count the vowels in a string. The function will take the string as an argument and will return the total number of vowels that the string has.
See the implementation in the below example:
// C program to count vowels in a string using pointers #include <stdio.h> // Function declaration int countVowels(char * ); int main() { char str[100]; int vowelsCount; printf("Enter a string: "); gets(str); // Call the countVowels() function vowelsCount = countVowels(str); // Print the count of vowels printf("The number of vowels in the string are: %d", vowelsCount); return 0; } // User-defined function to count vowels int countVowels(char * ptr) { int count = 0; // Runs until last char is reached while ( * ptr != '\0') { // Check if current character is a vowel(a,e,i,o,u) upper or lower case if ( * ptr == 'a' || * ptr == 'e' || * ptr == 'i' || * ptr == 'o' || * ptr == 'u' || * ptr == 'A' || * ptr == 'E' || * ptr == 'I' || * ptr == 'O' || * ptr == 'U') { count++; } // Move pointer to next character ptr++; } return count; }
Output:
Enter a string: Hello, how are you? The number of vowels in the string are: 7
Thanks for reading.