Hey guys, I am having trouble doing the following program:
Consider a sequence of words formed by letters. The words are separated with dashes, which can appear also at the beginning and in the end of the sequence. The sequence ends with a dot.
Write a program that prints the number of short words (less than five letters),
medium (between five and nine letters), and long (more than nine letters).
Input
The input consists of a line with letters and dashes, finished with a dot.
Output
Your program must print a linewith the number of short,medium and long words separated by commas.
Using strings is not allowed in this problem.
Now here is my code, which doesen't work on some test that the evaluator does.
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
|
#include <iostream>
using namespace std;
int main () {
char c;
int sh, med, lg; //will keep the count of how many words of each kind here
sh = 0;
med = 0;
lg = 0;
cin >> c;
int count;
if (c == '.') cout << sh << "," << med << "," << lg << endl; //if the first character is a dot, finish
else {
bool first = false;
if (c != '-') { // if the first character is not a dash, start the count at 1
first = true;
}
while (c != '.') {
if (first) { //the first character was not a dash
count = 1;
}
else count = 0;
first = false;
cin >> c; //imput second character
if (c != '.') { //if it is not a dot, enter the loop
while (cin >> c and c != '.') {
if (c == '-') { //if it is not a letter, check the previous count and reset the count
if (count > 0 and count < 5) ++sh;
if (count >= 5 and count < 9) ++med;
if (count >= 9) ++lg;
count = 0;
}
else ++count; //if it is a letter, increment the count
}
}
if (count > 0 and count < 5) ++sh; //check which size is the count
if (count >= 5 and count < 9) ++med;
if (count >= 9) ++lg;
}
cout << sh << "," << med << "," << lg << endl;
}
}
|
Some output examples:
Imput: -----abcde-. Output: 0,1,0
aaabbbccc. 0,1,0
----------------. 0,0,0
Thanks a lot!