In: Computer Science
Please, write code in c++. Using iostream library
Most modern text editors are able to give some statistics about
the text they are editing. One nice statistic is the average word
length in the text. A word is a maximal continuous sequence of
letters ('a'-'z', 'A'-'Z'). Words can be separated by spaces,
digits, and punctuation marks. The average word length is the sum
of all the words' lengths divided by the total number of
words.
For example, in the text "This is div2 easy problem". There are 5 words: "This"is"div"easy" and "problem". The sum of the word lengths is 4+2+3+4+7 = 20, so the average word length is 20/5 = 4.
Given a text, return the average word length in it. If there are no words in the text, return 0.0.
Input
The first line will contain the text of length between 0 and 50
characters inclusive. Text will contain only letters ('a'-'z',
'A'-'Z'), digits ('0'-'9'), spaces, and the following punctuation
marks: ',', '.', '?', '!', '-'. The end of text will be marked with
symbol '#' (see examples for clarification).
Output
Output should contain one number - the average length. The returned
value must be accurate to within a relative or absolute value of
10-9.
Samples:
№ | Input | Output |
---|---|---|
1 | This is div2 easy problem.# | 4.0 |
2 | a bc# | 1.5 |
3 | w84lFC1hD2ot2?43 Jnw67Kmt8KhOQn# | 2.714285714 |
#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int calcMin(int * array, int n){
int min= INT_MAX;
for(int i=0;i<n;i++)
if((array[i] != -1) && min
> array[i])
min= array[i];
return min == INT_MAX ? -1 : min;
}
int findMin(const string& s, int q){
int a = s.find(' ',q);
int b = s.find(',',q);
int c = s.find('.',q);
int d = s.find('?',q);
int e = s.find('!',q);
int f = s.find('-',q);
int g = s.find('0',q);
int h = s.find('1',q);
int i = s.find('2',q);
int j = s.find('3',q);
int k = s.find('4',q);
int l = s.find('5',q);
int m = s.find('6',q);
int n = s.find('7',q);
int o = s.find('8',q);
int p = s.find('9',q);
int r = s.find('#',q);
int array [1000] = {a,b,c,d,e,f, g,h,i,j,k,l,m,n,o,p,
r};
int w = calcMin(array, 17);
return w;
}
void split(const string& s,
vector<string>& v) {
string::size_type i = 0;
string::size_type j = findMin(s, 0);
while (j != string::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
// j = s.find('|', j);
j = findMin(s, j);
if (j == string::npos)
v.push_back(s.substr(i, s.length()));
}
}
string convertToString(char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
if(a[i]=='#')
break;
s = s + a[i];
}
return s;
}
int main() {
vector<string> v;
char a[50];
scanf("%[^\n]%*c", a);
string s = convertToString(a,50);
split(s, v);
float count = 0;
float sum = 0;
for (int i = 0; i < v.size(); ++i) {
if(v[i].size()){
count++;
sum += v[i].size();
}
}
float result = 0.0;
if(count)
result = sum/count;
int f = result;
if(f== result){
printf("%.1f", result);
} else {
printf("%.9f", result);
}
return 0;
}