Pagini recente » Cod sursa (job #1086966) | Cod sursa (job #2878091) | Cod sursa (job #3258208) | Cod sursa (job #2858829) | Cod sursa (job #2140406)
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
ifstream in("intrare.txt");
ofstream out("iesire.txt");
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;
}
int countLetter(std::string text)
{
int nLetter=0;
for (int i = 0; i < text.length(); i++)
{
if (isalpha(text.at(i)))
nLetter++;
}
return nLetter;
}
int main()
{
string buffer;
int numberWords;
getline(in, buffer);
numberWords = CountWords(buffer);
int numberOfLetter = countLetter(buffer);
//cout << numberWords << " " << numberOfLetter << endl;
out << (int)(numberOfLetter/ numberWords);
system("pause");
return 0;
}