Friday, July 31, 2009

I need help with my c++ program?

This HW is regarding strings. The sample output looks like this





Enter your lines:


Write a C program that will process a series of "words"


entered by the user. A word (for the purpose of this


assignment) is defined as a sequence of non-white-space


characters (anything other than a space, a tab, or a


newline). The following activities should be performed


in the program, and on the data entered by the user.


Does all of this sound right? You betcha!





There were 66 words found.


The longest word was 'non-white-space'.


The longest length was 15 characters.


The shortest word was 'a'.


The shortest length was 1 characters.


There were 6 capitalized words.


There were 5 sentences.


The longest sentence had 26 words in it.


The shortest sentence had 2 words in it.


The average word length was 4 characters.





I just need help finding number of words in the longest and shortest sentence. And the average word length of all the sentences.


I just need h

I need help with my c++ program?
// I have made a few changes to your program. Here is what


// I have:








#include %26lt;iostream.h%26gt;





int main()


{


string word, largest, smallest;


int numWords = 0;


int capital = 0;


int sentences = 0;


int currentSentenceWords = 0;


int smallestSentence = 0;


int largestSentence = 0;


char lastchr;





// To the following while-loop condition you need to add


// something that would make the loop termintate such as:


// while (cin %26gt;%26gt; word %26amp;%26amp; word != "-1")


// so if the user enters -1 then there are no more words or


// sentences to be entered and so your program can display


// the results and terminate. It's up to you what the termintating


// condition should be.





while (cin %26gt;%26gt; word)


{


//cout %26lt;%26lt; words %26lt;%26lt; endl;


numWords++;


currentSentenceWords++;





if (word[0] %26gt;= 'A' %26amp;%26amp; word[0] %26lt;= 'Z')


capital++;





lastchr = word[word.length() - 1];


if(lastchr == '.' || lastchr == '?' || lastchr == '!')


{


sentences++;


if(sentences == 1)


smallestSentence = largestSentence = currentSentenceWords;


else if(currentSentenceWords %26lt; smallestSentence)


smallestSentence = currentSentenceWords ;


else if(currentSentenceWords %26gt; largestSentence)


largestSentence = currentSentenceWords;


currentSentenceWords = 0;


}





// NOTE THAT I HAVE CHANGED THE FOLLOWING IF


// STATEMENT FROM if(numWords == 0) TO


// if(numWords == 1)





if(numWords == 1)


largest = smallest = word;


else if(word.length() %26gt; largest.length())


largest = word;


else if(word.length() %26lt; smallest.length())


smallest = word;


}





return 0;


}








// To calculate the average just divide the number of words by


// the number of sentences (numWords / sentences)





// If you need more help please let me know.

flower garden

No comments:

Post a Comment