C Program to Find Index Number of Vowels in a String

Introduction

In this post, I am going to write a c program to find the index number of vowels in a string. Here, we will read a string from the console through the keyboard. After reading the string, this program will display index numbers of all the vowels present in the string.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

See the following expected output.

Expected Output

Enter any string
HPLUS ACADEMY for C Programming
U is found at 4
A is found at 7
A is found at 9
E is found at 11
o is found at 16
o is found at 23
a is found at 26
i is found at 29

As you can see, this program is asking user to enter any string. After that, you can find the position of each and every vowel from the string.

Also Read: C Program to Print Multiples of 5 using do while loop

Let us see the logic of this program.

Logic to Find Index of Vowels in C Programming

  1. Ask the user to enter any string.
  2. Using the getchar() function, we will read and every character and check whether this character is a vowel or not.
  3. For this, I have used a switch case in c programming. There will be total 10 cases. Because c programming is case-sensitive, i.e. ‘a’ and ‘A’ are not the same in c programming.
  4. So, 5 cases are for each small case vowel and the remaining 5 cases are for the capital vowels.
  5. The switch case is written inside the while loop. We don’t know the length of the string so we will continue to execute the loop until we get ‘\n’ i.e. user press enter.
  6. Here, we will integer variable ‘index’ to display the position of the vowel.
  7. Before entering the body of the switch, we will increment this variable index by 1.
  8. Whenever we get the vowel, the control will go to the respective case, and the value of this variable ‘index’ will be displayed.

I hope you have understood the logic of the program. Let’s see the actual c program to find index number of vowels in a string.

C Program to Find Index Number of Vowels in a String

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char ch;
    int index=0;
    printf("Enter any string\n");
    while((ch=getchar())!='\n')
    {
        index++;
        switch(ch)
        {
        case 'a':
            printf("a is found at %d\n",index);
            break;
        case 'A':
            printf("A is found at %d\n",index);
            break;
        case 'e':
            printf("e is found at %d\n",index);
            break;
        case 'E':
            printf("E is found at %d\n",index);
            break;
        case 'i':
            printf("i is found at %d\n",index);
            break;
        case 'I':
            printf("I is found at %d\n",index);
            break;
        case 'o':
            printf("o is found at %d\n",index);
            break;
        case 'O':
            printf("O is found at %d\n",index);
            break;
        case 'u':
            printf("u is found at %d\n",index);
            break;
        case 'U':
            printf("U is found at %d\n",index);
            break;
        }
    }
    return 0;
}

I have not used default case here because there is no need. If you have any difficulty regarding this program, then please let me know.

Thank you.

Some Important C Programs

  1. C Program to Remove Zeros from a number
  2. The while loop in C Programming
  3. Reverse a Number using getchar and putchar function in c
  4. C Program to Print Numbers Except Multiples of n
  5. C Program to Remove White Spaces and Comments from a File
  6. Program in C to Find Longest Line in a File
  7. Palindrome in C using Pointers
  8. Insert and Delete element in Array in C using switch case
  9. C Program to Add Alternate Elements of a 2D Array
  10. Arrays in C for Complete Beginners
  11. C Program to Find Area of a Circle using Preprocessor

Leave a Comment