Question

Write a function that count the number of vowels, the number of consonants and the average number of letters in each word. The function accept a C-String (char array) or a string object as an argument and return the number of vowels, number of consonants and the average number of letters in each word. Problem: Requirements: . Use pointers as part of the solution Read a string from a text file. . Write in a text file the string, the number of vowels, the number of consonants and the average number of letters in each word.

I need this in Visual Studio C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <fstream>
#include <string>
#include <iostream>
#include <cwctype>
#include <cctype>

using namespace std;

string trim(string &str) {
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
str.erase(str.find_last_not_of(' ')+1); //surfixing spaces
return str;
}

bool is_vowel(char c) {
c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

bool is_consonant(char c) {
return !is_vowel(c) && isalpha(c);
}

void writeStatistics(string content) {
content = trim(content);
int vowels = 0, consonants = 0;
int words = 0;
bool lastSpace = false;
  
if(content.length() != 0) {
words = 1;
lastSpace = true;
  
for(int i=0; i<content.length(); i++) {
char c = content.at(i);
if(iswspace(c)) {
if(!lastSpace) {
words++;
}
lastSpace = true;
} else if(is_vowel(c)) {
vowels++;
lastSpace = false;
} else if(is_consonant(c)) {
consonants++;
lastSpace = false;
}
}
}
  
  
ofstream myfile;
myfile.open ("output.txt");
myfile << "Vowels: "<< vowels << "\n";
myfile << "Consonants: "<< consonants << "\n";
myfile << "Words: "<< words << "\n";
myfile.close();
}


int main(int argc, char** argv) {

ifstream ifs("input.txt");
  
// populate file data in content variable
string content( (istreambuf_iterator<char>(ifs) ), (istreambuf_iterator<char>()));

writeStatistics(content);
  
return 0;
}

========================================

Please save your data in a file input.txt and the statistics will be written to output.txt.

saved share main.cpp input.txt output.txt #include #include #include #include #include <fstream> <string> <iostream> <CNCtype

Add a comment
Know the answer?
Add Answer to:
I need this in Visual Studio C++ Write a function that count the number of vowels,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT