C program to print vowels in a string

(Last Updated On: March 1, 2019)

C program to print vowels in a string and their location. We need to find all vowels (lower case and upper case) in a given string and its location.

 

For example,

Input: Hello world

Output:

Vowel: e at location: 1
Vowel: o at location: 4
Vowel: o at location: 7

Solution to find vowels in String

Solution is very simple, we can store string into a char array. Retrieve each char using a for loop and compare them with all vowels i.e. a,e,i,o,u,A,E,I,O,U . If character is matching, we can print the locating and character.

 

NOTE:

To read the input string e.g. Hello World, in scanf function, we need to use the expression scanf(“%[^\n]s”, str); – notice the [^\n]. It will read the string including space. If we use simple like scanf(“%s”, str);to read string, then only first word e.g. in hello world, only hello will be read by scanf.

 

C program to print vowels in a string and their locations.