SHARE
TWEET

Untitled

a guest Jul 19th, 2019 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. void preloadFile(const char[]);
  7.  
  8. int main() {
  9.     const char file[] = "test.txt";
  10.     string buffer;
  11.     int wordcount = 0, lettercount = 0, fourletterwords = 0, tempcount = 0, temppos, wordWithLetterCount = 0;
  12.     char letter;
  13.    
  14.     preloadFile(file);
  15.    
  16.     ifstream in(file);
  17.     if (in.fail()) {
  18.         in.close();
  19.         return 1;
  20.     }
  21.    
  22.     cout << "Count letters that begin with which letter? ";
  23.     cin >> letter;
  24.    
  25.     while (in >> buffer) {
  26.         wordcount++;
  27.         tempcount = 0;
  28.         temppos = 0;
  29.        
  30.         for (int i = 0; i <= buffer.length(); i++) {
  31.             if (isalpha(buffer[i])) {
  32.                 lettercount++;
  33.                 tempcount++;
  34.             }
  35.             else if (buffer[i] != '\'') {
  36.                 cout << buffer.substr(temppos, i-temppos) << " " << tempcount;
  37.                 if (buffer[temppos] == letter) {
  38.                     cout << " (BEGINS WITH \'" << letter << "\')";
  39.                     wordWithLetterCount++;
  40.                 }
  41.                 if (tempcount == 4) {
  42.                     cout << " (FOUR LETTERS)";
  43.                     fourletterwords++;
  44.                 }
  45.  
  46.                 cout << endl;
  47.                 tempcount = 0;
  48.                 temppos = i+1;
  49.             }
  50.         }
  51.     }
  52.    
  53.     cout << "There are " << wordcount << " total words." << endl
  54.         << "There are " << lettercount << " total letters." << endl
  55.         << "There are " << fourletterwords << " total four letter words." << endl
  56.         << "There are " << wordWithLetterCount << " total words that begin with \'" << letter << "\'" << endl;
  57.        
  58.     system(((string)"notepad " + file).c_str());
  59.     return 0;
  60. }
  61.  
  62. void preloadFile(const char file[]) {
  63.     ofstream out(file, ios::out|ios::trunc);
  64.     out << "she said \"hello, my name's Jane\" and then she walked away.";
  65.     out.close();
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top