Skip to content

Instantly share code, notes, and snippets.

@JoinedSenses
Last active July 8, 2019 14:00
Show Gist options
  • Save JoinedSenses/939346a1a4f091f248630150c717baef to your computer and use it in GitHub Desktop.
Save JoinedSenses/939346a1a4f091f248630150c717baef to your computer and use it in GitHub Desktop.
Text Analysis
#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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment