help

I have completed half of this program, and i don't know how to do the rest half, I have to submit this by tonight, could anyone please help me out with this?


How would i count the average letters per word in a sentence??


#include <iostream>
#include <string>
using namespace std;

int WordCount(string);
int letterCount(string);
int main()
{

string Mystring;

cout << " Type in the string you would like to use: " << endl;

getline(cin, Mystring); // inputs sentence

cout << " Your string is: " << Mystring << endl;



cout << " The string has " << WordCount(Mystring) << " words " << endl;

cout << " the avg letters per word is " << letterCount(Mystring);


system("pause");
return 0;
}


// counts the words
int WordCount(string Sentence)
{
int length = Sentence.length(); // gets lenght of sentence and assines it to int length

int words = 1;


for (int size = 0; length > size; size++)
{

if (Sentence[size] == ' ' && Sentence[size-1] != ' ')
/*space*/
words++; // ADDS TO words if there is a space
}
if ( Sentence[0] == ' '/*space*/ )
words--;
return words;
}

int letterCount(string)
{
int avgNoOfLetters;



return avgNoOfLetters;
}


Sum the number of letters then divide by the number of words.
do u know whats the syntax to add number of letters? thats where i am getting confused.

Also if i sum the letters i will have to make another loop which subtracts the spaces right?
Do you know how to add in C++? You can use that with a for or while loop over the sentence.
yes yes, i know how to add using for/while loop but i dont know how to add


i tried doing this but i am not getting prefered answer

float letterCount(string sentence)
{
int length = sentence.length(); // gets lenght of sentence and assines it to int length
float avgNumber;

int words = 1;


for (int size = 0; length > size; size++)
{

if (sentence[size] == ' ' && sentence[size-1] != ' ')
/*space*/
words++; // ADDS TO words if there is a space

}
if ( sentence[0] == ' '/*space*/ )
words--;

avgNumber = length/words;

return avgNumber;
}




The question in the problem gives us an example output and this is how it looks

Enter a string: Four score and seven years ago
There are 6 words in that sentence.
The average number of letters per word is 4.2


(don't worry about the format, i can fix that, i just want the "4.2" to come properly)
i even tried this avgNumber = float(length/words)

and changed everything to float

float length = sentence.length(); // gets lenght of sentence and assines it to int length
float avgNumber;

float words = 1

the answer i am getting for avg letters per word is 5
Last edited on
i think i have to subtract the number of spaces in the sentence, i am working on how to to that, and tips?
To get the average number of words per sentence, you need to sum the number of letters in each word then divide by the number of words. Your answer looks very close; I think you just need to subtract spaces as you mentioned before.
yes, any tips on how do i subtract spaces


(i counted the number of spaces and hardcoated (length-5)/words

i get a correct answer

how do i subract noOfSpaces! thanks for ur help man really appreciate it, where are u from sir?
I got it! if anyone wants to look at my code, maybe add to it or give any suggestions, be my guest! Cheerz happy coding!



#include <iostream>
#include <string>
using namespace std;

int WordCount(string);
float letterCount(string);
int main()
{

string Mystring;

cout << " Enter a string: ";

getline(cin, Mystring); // inputs sentence

//cout << endl << " Your string is: " << Mystring << endl;



cout << "There are "<< WordCount(Mystring) << " words in that sentence." << endl;

cout << "The average number of letters per word is " << letterCount(Mystring) << endl;


system("pause");
return 0;
}


// counts the words
int WordCount(string Sentence)
{
int length = Sentence.length(); // gets lenght of sentence and assines it to int length

int words = 1;


for (int size = 0; length > size; size++)
{

if (Sentence[size] == ' ' && Sentence[size-1] != ' ')
/*space*/
words++; // ADDS TO words if there is a space
}
if ( Sentence[0] == ' '/*space*/ )
words--;
return words;
}

float letterCount(string sentence)
{
float length = sentence.length(); // gets lenght of sentence and assines it to int length
float avgNumber;
int spaces =0;

float words = 1;


for (int size = 0; length > size; size++)
{

if (sentence[size] == ' ' && sentence[size-1] != ' ')
{/*space*/
words++;
spaces++;// ADDS TO words if there is a space
}
}
if (sentence[0] == ' '/*space*/ )
{ words--;

}

avgNumber = float((length-spaces)/words);

return avgNumber;
}
Topic archived. No new replies allowed.