Advertisement
homeworkhelp111

Lab17c

Nov 12th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. bool isVowel(char ch) {
  7.     bool result = false;
  8.     //Check if the character is a,e,i,o,u
  9.     if(ch == 'a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' || ch=='o' || ch=='O' || ch=='u' || ch=='U') {
  10.         result = true;
  11.     }
  12.     return result;
  13. }
  14.  
  15. int countVowels(string word) {
  16.     int count = 0;
  17.    
  18.     //Check each character of the word by calling isVowel function
  19.     for(int j=0; j<word.length(); j++) {
  20.         if(isVowel(word[j])) {
  21.             count++;
  22.         }
  23.     }
  24.    
  25.     return count;
  26. }
  27.  
  28. int countWords(string line) {
  29.     int count = 0;
  30.    
  31.     //Check each character to see if there is a space
  32.     for(int j=0; j<line.length(); j++) {
  33.         if(line[j] == ' ') {
  34.             count++;
  35.         }
  36.     }
  37.    
  38.     return count+1;
  39. }
  40.  
  41. int main() {
  42.     int vowelCount, wordCount;
  43.    
  44.     ifstream file("Lab17C.txt");
  45.     string line;
  46.     getline(file, line);
  47.    
  48.     vowelCount = countVowels(line);
  49.     wordCount = countWords(line);
  50.    
  51.     cout <<"Number of vowels: "<< vowelCount << endl;
  52.     cout <<"Number of words: "<< wordCount << endl;
  53.    
  54.     return 1;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement