1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
//Michael Ervin - A program to count different parts of a string
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void introduction();
bool vInput(string temp, char& tempPunc, int len);
void remCommas(string& temp);
int vCounter(string temp);
int cCounter(string temp);
int lWord(string temp);
int sWord(string temp);
void Output(int V, int C, int L, int S, char P);
int main()
{
string input;
int len, numVowels = 0, numCons = 0, longWord, shortWord;
char pMark = '\0';
bool gString = false;
introduction();
do
{
getline(cin, input);
len = input.length()-1;
gString = vInput(input, pMark, len);
}
while (gString == false);
input = input.substr(0, len);
remCommas(input);
numVowels = vCounter(input);
numCons = cCounter(input);
longWord = lWord(input);
shortWord = sWord(input);
Output(numVowels, numCons, longWord, shortWord, pMark);
cout << "Press enter to exit. ";
cin.get();
return 0;
}
void introduction()
{
cout << "----------------------------------------------------------\n" <<
"Please enter a sentence that ends with a punctuation mark.\n" <<
"No special characters please, letters and , . ! ?\n" <<
"----------------------------------------------------------\n\n";
}
bool vInput(string temp, char& tempPunc, int len)
{
if (temp[len] == '.' || temp[len] == '?' || temp[len] == '!')
{
tempPunc = temp.at(len);
return true;
}
cout << "\nSorry, your sentence does not end with a punctuation mark\n" << "Please enter a different sentence" << endl;
return false;
}
void remCommas(string& temp)
{
int position;
if (temp.find(',') != -1)
{
position = temp.find(',');
while (position != -1)
{
temp.erase(position, 1);
position = temp.find(',', position + 1);
}
}
}
int vCounter(string temp)
{
unsigned int i;
int vc = 0;
for (i = 0; i <= temp.length(); i++)
{
if (toupper(temp[i]) == 'A' || toupper(temp[i]) == 'E' ||
toupper(temp[i]) == 'I' || toupper(temp[i]) == 'O' ||
toupper(temp[i]) == 'U')
vc++;
}
return vc;
}
int cCounter(string temp)
{
unsigned int i;
int cc = 0;
for (i = 0; i <= temp.length(); i++)
{
if ((temp[i] >= 66 && temp[i] <= 90) || (temp[i] >= 98 && temp[i] <= 122))
if (temp[i] != 69 && temp[i] != 73 && temp[i] != 79 &&temp[i] != 85 &&
temp[i] != 101 && temp[i] != 105 && temp[i] != 111 && temp[i] != 117)
cc++;
}
return cc;
}
int lWord(string temp)
{
int position, longest = 0;
temp.append(1, ' ');
while (temp.size() != 0)
{
position = temp.find(' ', 0);
if (position >= longest)
longest = position;
temp = temp.substr(position + 1, temp.length() - 1);
}
return longest;
}
int sWord(string temp)
{
int position, shortest = temp.length();
temp.append(1, ' ');
while (temp.size() != 0)
{
position = temp.find(' ', 0);
if (position <= shortest)
shortest = position;
temp = temp.substr(position + 1, temp.length() - 1);
}
return shortest;
}
void Output(int V, int C, int L, int S, char P)
{
cout << endl;
cout << "Your sentence has " << V << " vowels." << endl;
cout << "Your sentence has " << C << " consonants." << endl;
cout << "Your longest word is " << L << " characters long." << endl;
cout << "Your shortest word is " << S << " characters long." << endl;
cout << "The punctuation mark you used was: " << P << endl;
cout << endl;
}
|