Instructions:
Using a do…while() loop, continuously scan for characters (one per line) and print it out afterwards. Remember to place a space before the character's placeholder when scanning so that the newline characters will be ignored and the correct values will be scanned.
The loop shall terminate due to either of the following reasons:
The inputted character is a vowel
The number of inputted characters has already reached 5.
For all of the test cases, it is guaranteed that if the number of inputted characters is less than 5, then there must be a vowel from among the inputted characters. Also, it is guaranteed that all the characters are in lowercase.
Input
1. A series of characters
Output
Multiple lines containing message prompts for characters.
Enter·a·character:·c
c
Enter·a·character:·f
f
Enter·a·character:·a
a
#include <stdio.h>
#include <stdbool.h> // not necessary but "bool isVowel" looks better
int main()
{
int i = 0;
char letter;
bool isVowel;
do{
printf("Enter a character: ");
scanf(" %c", &letter);
printf("%c \n", letter);
switch(letter){
case 'a': case 'e': case 'i': case 'o': case 'u': case 'y':
isVowel = true;
break;
default:
isVowel = false;
}
i++;
} while(i<5&&!isVowel);
return 0;
}
Comments
Leave a comment