C program to find the last vowel in a string

C program to find the last vowel in a string :

In this tutorial, we will learn how to find the last vowel in a string. For example, for the string, Hello Everyone, the last vowel is e. Our program will print this value. The user will enter one string and the program will check each character one by one. If any vowel found, it will store that character value. Again if another vowel found, it will replace the previous one with the new character. Finally, after the string is completed, print out the character. If no character is found, print one different message. Let’s take a look at the C program first :

C program :

#include <stdio.h>

//7
int isVowel(char c)
{
    //8
    switch (c)
    {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U':
        return 1;
    default:
        return 0;
    }
}

int main()
{
    //1
    char inputString[100];
    int i;
    int flag = 0;
    char vowel;

    //2
    printf("Enter your string : ");
    fgets(inputString, 100, stdin);

    //3
    for (i = 0; inputString[i] != '\n'; i++)
    {
        //4
        if (isVowel(inputString[i]))
        {
            //5
            vowel = inputString[i];
            flag = 1;
        }
    }

    //6
    if (!flag)
    {
        printf("No vowel found.\n");
    }
    else
    {
        printf("Last vowel in the string : %c \n", vowel);
    }
}

Explanation :

The commented numbers in the above program denote the step number below :

  1. Create one char array variable to store the user input string. One integer i to use in loop, integer flag to detect if any vowel is found or not. It’s value is 0 if no vowel found. If any vowel will be detected, we will change its value to 1 .Also create one char variable vowel to store vowel-character if found.
  2. Ask the user to enter one string and store it in inputString variable.
  3. Start one for loop and scan each character of the string one by one. It will run till a new line is found , i.e. string-end is reached.
  4. Check if the current character is vowel or not. We are using inputString function to check this.
  5. If current character is vowel, set the value of this character to variable vowel and mark the value of flag as 1.
  6. Finally, check the value of flag. If it is still 0, no vowel is found. Print the message. Else print the vowel or last-vowel found.
  7. isVowel function takes one character as input. It checks if this character is vowel or not. If vowel, it returns 1, else returns 0 .
  8. Using a switch case we are checking if the provided character is vowel. If yes, return 1, else return 0 .We are checking for both lower-case and upper-case letters.

Sample Output :

Enter your string : This is a stringu
Last vowel in the string : u

Enter your string : vowel
Last vowel in the string : e

Enter your string : bcd
No vowel found.

Enter your string : a b c d e f g h i j k l m n o p q r s t u v w x y z
Last vowel in the string : u