Design a program that prompts the user to enter a string. The program should the
ID: 3533750 • Letter: D
Question
Design a program that prompts the user to enter a string. The program should then display the number of vowels and the number of consonants in the string. I am absolutely lost and don't even know how to start, any assistance would be appreciated.
I will also provide the pseudocode that was provided by the teacher, if it helps at all:
// main module
Module main()
// Variables
Declare String str
Declare Integer index
Declare Integer vowels = 0
Declare Integer consonants = 0
// Get input from the user.
Display "Enter a string."
Input str
// Scan the string counting vowels and consonants.
For index = 0 To length(str) - 1
If isVowel(str(index)) Then
Set vowels = vowels + 1
Else If isConsonant(str(index)) Then
Set consonants = consonants + 1
End If
End For
// Display the results.
Display "Vowels: " , vowels
Display "Consonants: " , consonants
End Module
// The isVowel function returns True if the argument
// is a vowel, or False otherwise.
Function Boolean isVowel(String ch)
Declare Boolean status // Flag
// Convert the argument to uppercase.
ch = toUpper(ch)
// Is ch a vowel?
If ch == "A" OR ch == "E" OR
ch == "I" OR ch == "O" OR
ch == "U" Then
Set status = True
Else
Set status = False
End If
Return status
End Function
// The isConsonant function returns True if the argument
// is a consonant, or False otherwise.
Function Boolean isConsonant(String ch)
Declare Boolean status // Flag
// Is ch a letter?
If isLetter(ch) Then
// Is ch not a vowel?
If NOT isVowel(ch) Then
Set status = True
Else
Set status = False
End If
Else
Set status = False
End If
Return status
End Function
Explanation / Answer
#include<stdio.h>
#include<conio.h>
main()
String str[];
Int index;
Int vowels= 0;
Int consonants = 0;
printf("enter a string");
scanf("%s",&str);
// Scan the string counting vowels and consonants.
For (index = 0 ;index< length(str) - 1;index++)
{
If( isVowel(str(index))
vowels = vowels + 1;
Else
If (isConsonant(str(index))
consonants = consonants + 1;
}
printf("no of vowel in string %d",vowels);
printf("no of consonents in string %d",consonants);
}
// The isVowel function returns True if the argument
// is a vowel, or False otherwise.
Boolean isVowel(String ch)
{
Boolean status ;
// Convert the argument to uppercase.
ch = toUpper(ch)
// Is ch a vowel?
If( ch == "A" || ch == "E" ||
ch == "I" ||ch == "O" ||
ch == "U" )
status = True
Else
status = False
Return status
}
// The isConsonant function returns True if the argument
// is a consonant, or False otherwise.
Boolean isConsonant(String ch)
{
Boolean status
If (isLetter(ch))
{
If (ch! = "A" || ch! = "E" ||
ch! = "I" ||ch != "O" ||
ch != "U")
status = True
Else
status = False
}
Else
status = False
}
Return status
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.