Last Vowel

🎯 Array Hurdle

Last Vowel

Difficulty: ⚫◯◯◯

Tags: array, characters, search

Hurdle: Passing this question is sufficient to pass the arrays hurdle.

Description

Write a function last_vowel that takes a character array with exactly size elements.

The function should return the last vowel in the array.

If there are no vowels in the array, return '\0' (the null character).

Vowels: a, e, i, o, u (and their uppercase versions A, E, I, O, U)

Examples

Example 1

Input: {'h', 'e', 'l', 'l', 'o'}
Output: 'o'

Explanation: The last vowel in the array is 'o' at index 4.

Example 2

Input: {'b', 'a', 'n', 'a', 'n', 'a'}
Output: 'a'

Explanation: The last vowel is 'a' at index 5.

Example 3

Input: {'x', 'y', 'z'}
Output: '\0'

Explanation: No vowels in the array, so return the null character.

Example 4

Input: {'H', 'E', 'L', 'L', 'O'}
Output: 'O'

Explanation: Uppercase vowels count too. The last vowel is 'O'.

Example 5

Input: {'c', 'a', 't'}
Output: 'a'

Explanation: Only one vowel, 'a', so return it.

Example 6

Input: {'a', 'e', 'i', 'o', 'u'}
Output: 'u'

Explanation: All vowels, the last one is 'u'.

Function Signature

char last_vowel(int size, char array[MAX_COLS]);

Constants

#define MAX_COLS 100

Constraints

  • The array has at least 1 character.
  • The array has at most 100 characters.
  • Uppercase and lowercase vowels all count as vowels.
  • If no vowels exist, return '\0'.
  • Do not modify the array provided.
  • Do not call scanf, getchar, or fgets.
  • Do not call printf.

Hints

There are two approaches:

Approach 1: Traverse from beginning to end, update your answer every time you find a vowel. The last update will be the last vowel.

Approach 2: Traverse from end to beginning, return immediately when you find a vowel.

Testing

./last-vowel hello
o

./last-vowel banana
a

./last-vowel xyz
(null character - no output or special output)

./last-vowel HELLO
O

./last-vowel cat
a
Code Editor
Output
Run your code to see output here...