C++ Calculate number of characters, words, sentences
C++ Calculate number of characters, words, sentences
#include <iostream>
#include <sstream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
int i = 0, count = 0,a=0;
char text[256];
vector<string> sV;
cout<<" Welcome to the program 2.4 written by Your Name"<<endl;
cout<<"********************************************************************************"<<endl;
cout<<endl<<endl<<endl;
cout << "Enter text (max. 256 characters): ";
cin.getline(text, 256, '\n');
string s(text), word;
cout << "Your text was: " << endl;
for (i=0; i<s.size(); i++){
if (i == 0) {
s[i] = toupper(s[i]);
cout << s[i];
}
if (isalpha(s[i])){
if (s[i-1] =='?' || s[i-1] == '!' || s[i-1] =='.') {
s[i] = toupper(s[i]);
cout << s[i];
}
else{
s[i] = tolower(s[i]);
cout << s[i];
}
}
else if (isdigit(s[i])){
cout << s[i];
}
else if (isspace(s[i])){
if (s[i-1] != ' ' || s[i+1] != ' ') {
cout << s[i];
}
}
else if (s[i] =='?' || s[i] == '!' || s[i] =='.'){
if (s[i+1] != ' ' || s[i+1] != ' ') {
cout << s[i] << " ";
}
else {
cout << s[i];
}
count=count+1;
}
}
while(s[i] ==' ' || s[i] == '!' || s[i] =='.')
{
cout << s[i];
a=a+1;
}
cout << endl;
istringstream instr(s);
while (instr >> word){
sV.push_back(word);
}
cout << "Number of characters in text: " << s.length() << endl;
cout << "Number of words in text: " <<sV.size() << endl;
cout << "Number of sentences in text: " << count << endl;
system ("pause");
return 0;
}
Output
Welcome to the program 2.4 written by Your Name
***********************************************
*********************************
Enter text (max. 256 characters): Your text was:
Number of characters in text: 0
Number of words in text: 0
Number of sentences in text: 0
sh: pause: command not found