Check and Count vowel Character in String
For this one, we’re going to play with some characters and strings.
The first function I want you to write takes a single character and returns whether or not it is a vowel (‘a’, ‘e’, ‘I’, ‘o’, ‘u’). Call the function is a vowel. It should return a 1 if the character sent to it is a vowel and a 0 if it is not. It should work for upper and lower case, and you can use to upper or to lower to help with this if you want.
Next, write another function called count_vowels. This one takes a string as its argument (i.e. an array of char) and returns the number of vowels in the string. “Alicia Morissette” should return 8. Obviously, it should use the function that you created in the first part.
Next declare three strings. They should probably be about 30 characters each. Ask the user to enter their first and last names, and then using strcpy and strcat create a single string (the third one of the ones you declared) which has the first and last names reversed and separated by a ‘,’. When you output this string also tell the person how many vowels they have in their name.
Finally set up a sentinel loop that reads in people’s names and keeps doing this process until the user types “quit” as either the first or the last name (or both). This last will involve using strcmp.
Once you have your program working do a sample run with the names “Alicia Morissette,” “John Lennon” and your own name. Create a text file of your sample run.
Solution:
#include < stdio.h>
#include < ctype.h>
int is_vowel(char c)
{
// convert it to lowercase
c = tolower(c);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' ||c == 'u')
return 1;
else
return 0;
}
int count_vowels(char arr[])
{
char c = '0';
int vowels = 0;
int i = 0;
// Iterate through the chars in the array
while(c != '\0')
{
c = arr[i];
if(is_vowel(c) == 1)
vowels++;
i++;
}
return vowels;
}
int main()
{
// Declare three strings
char str1[30];
char str2[30];
char str3[30];
printf("Enter your first name: ");
scanf("%s", str1);
printf("Enter your last name: ");
scanf("%s", str2);
// Create single strings
strcat(str3, str2);
for(int i = 0; i < 33; i++)
{
if(str3[i] == '\0')
{
str3[i] = ',';
str3[i+1] = '\0';
break;
}
}
strcat(str3, str1);
printf("%s", str3);
// Display vowels
printf("\nYour name contains %d vowels\n\n", count_vowels(str3));
// Sentinel loop
char input[100];
while(1)
{
char str3[30];
printf("Enter your first name: ");
scanf("%s", str1);
if(strcmp(str1, "quit") == 0 || strcmp(str1, "Quit") == 0)
break;
printf("Enter your last name: ");
scanf("%s", str2);
strcat(str3, str2);
for(int i = 0; i < 33; i++)
{
if(str3[i] == '\0')
{
str3[i] = ',';
str3[i+1] = '\0';
break;
}
}
strcat(str3, str1);
printf("%s", str3);
// Display vowels
printf("\nYour name contains %d vowels\n\n", count_vowels(str3));
}
return 0;
}
Output:
Enter your first name: John
Enter your last name: Lennon
Lennon, John
Your name contains 3 vowels
Enter your first name: Alicia
Enter your last name: Morissette
Morissette, Alicia
Your name contains 8 vowels