Convert the program in the file main.c from a C program to a C++ program (your f
ID: 670893 • Letter: C
Question
Convert the program in the file main.c from a C program to a C++ program (your file should have a .cpp extension). Convert all of the I/O to the C++ style I/O (using cin and cout). Be sure to maintain the same formatting that was used in the C program using the C++ equivalents. Replace the libraries <stdio.h>, <ctype.h>, and <string.h> with their C++ equivalents <iostream>, <cctype>, and <string>. Sample input/output of program:
Please enter the student's sentence: The quick brown fox jumps OVER the lazy dog.
Please enter the student number (6 digits): 012345
The sentence entered is:
The quick brown fox jumps OVER the lazy dog.
The sentence for the student with student number 012345 contains 35 letters and 11 vowels. 31.4% of the letters are vowels.
Convert the vowel test code into a function. The function signature should look like:
bool isVowel(char c)
Adjust the rest of the program to call the function to test whether or not the letter is a vowel and if so, increment vowelCount (the increment should be in main, not in the function).
Move the isVowel function into the namespace counter. Adjust the rest of the code to call the function from the namespace.
Convert the isVowel function into an inline function. The function signature should look like:
inline bool isVowel(char c)
Explanation / Answer
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string sentence;
int letterCount = 0, vowelCount = 0;
int studentNo;
int i;
cout << "Please enter the student's sentence: ";
cin >> sentence;
cout << "Please enter the student number (6 digits): ";
cin >> studentNo;
for (i = 0; i < sentence.length(); i++)
{
if (isalpha(sentence[i]))
{
letterCount++;
switch (toupper(sentence[i]))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowelCount++;
break;
}
}
}
cout << "The sentence entered is: " << sentence << endl;
cout << "The sentence for the student with student number "<< studentNo <<" contains " << letterCount<<" letters and " << vowelCount << " vowels. "<< (float)vowelCount/(letterCount*100) << " of the letters are vowels. ";
return 0;
}