Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- void preloadFile(const char[]);
- int main() {
- const char file[] = "test.txt";
- string buffer;
- int wordcount = 0, lettercount = 0, fourletterwords = 0, tempcount = 0, temppos, wordWithLetterCount = 0;
- char letter;
- preloadFile(file);
- ifstream in(file);
- if (in.fail()) {
- in.close();
- return 1;
- }
- cout << "Count letters that begin with which letter? ";
- cin >> letter;
- while (in >> buffer) {
- wordcount++;
- tempcount = 0;
- temppos = 0;
- for (int i = 0; i <= buffer.length(); i++) {
- if (isalpha(buffer[i])) {
- lettercount++;
- tempcount++;
- }
- else if (buffer[i] != '\'') {
- cout << buffer.substr(temppos, i-temppos) << " " << tempcount;
- if (buffer[temppos] == letter) {
- cout << " (BEGINS WITH \'" << letter << "\')";
- wordWithLetterCount++;
- }
- if (tempcount == 4) {
- cout << " (FOUR LETTERS)";
- fourletterwords++;
- }
- cout << endl;
- tempcount = 0;
- temppos = i+1;
- }
- }
- }
- cout << "There are " << wordcount << " total words." << endl
- << "There are " << lettercount << " total letters." << endl
- << "There are " << fourletterwords << " total four letter words." << endl
- << "There are " << wordWithLetterCount << " total words that begin with \'" << letter << "\'" << endl;
- system(((string)"notepad " + file).c_str());
- return 0;
- }
- void preloadFile(const char file[]) {
- ofstream out(file, ios::out|ios::trunc);
- out << "she said \"hello, my name's Jane\" and then she walked away.";
- out.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement