Pagini recente » Cod sursa (job #2130800) | Cod sursa (job #1924851) | Cod sursa (job #1820466) | Cod sursa (job #2634337) | Cod sursa (job #2140411)
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
ifstream in("intrare.txt");
ofstream out("iesire.txt");
short int CountWords(std::string strString)
{
int nSpaces = 0;
unsigned int i = 0;
// Skip over spaces at the beginning of the word
while (isspace(strString.at(i)))
i++;
for (; i < strString.length(); i++)
{
if (isspace(strString.at(i)))
{
nSpaces++;
// Skip over duplicate spaces & if a NULL character is found, we're at the end of the string
while (isspace(strString.at(i++)))
if (strString.at(i) == '\0')
nSpaces--;
}
}
// The number of words = the number of spaces + 1
return nSpaces + 1;
}
short int countLetter(std::string text)
{
int nLetter=0;
unsigned int i;
for ( i = 0; i < text.length(); i++)
{
if (isalpha(text.at(i)))
nLetter++;
}
return nLetter;
}
int main()
{
string buffer;
short int numberWords;
getline(in, buffer);
numberWords = CountWords(buffer);
short int numberOfLetter = countLetter(buffer);
//cout << numberWords << " " << numberOfLetter << endl;
out << (int)(numberOfLetter/ numberWords);
//system("pause");
return 0;
}