Programming Fundamentals Assignment No 03

Here is the Programming Fundamentals Assignment No 03

Question:

Write a program that obtains a paragraph from user as input and then determines and prints the following statistics:

  • Words Count – Total number of words in the paragraph
  • Characters Count – Total number of characters in the paragraph (excluding whitespaces)
  • Sentences Count– Total number of sentences in the paragraph

You need to create a separate function that should accept the entered paragraph as parameter, then process it to yield the aforementioned statistics, and then finally print them within the said function.

Code:

Here is the Programming Fundamentals Assignment No 03

using namespace std;
void paragraph(string, int &,int &, int &); // DECLRING THE PROTOTYPE OF THE FUNCTION

int main() // STARTING THE MAIN FUNCTION
{

string para;                        // DECLARING VARIABLE FOR INPUT FROM USER
cout<<"Enter the paragraph : ";     // PROMPTING THE USER FOR INPUT
getline(cin,para);                  // TAKING PARAGRAPH FROM USER AS INPUT

int words=0,characters=0,sentences=0;       // DECLARING VARIABLES FOR COUNTING WORDS, SENTENCES, AND CHARACTERS.

paragraph(para,characters,words,sentences);     // CALLING FUNCTION FROM THE MAIN

cout<<"Characters = "<<characters<<endl;        // DISPLAYING THE CHARACTERS IN THE PARAGRAPH
cout<<"Words = "<<words<<endl;                  // DISPLAYING THE WORDS IN THE PARAGRAPH
cout<<"Sentence = "<<sentences;                 // DISPLAYING THE SENTENCES IN THE PARAGRAPH

return 0;

}

void paragraph (string para, int &a,int &b, int &c) // HEADER OF THE FUNCTION
{

// LOOP FOR COUNTING THE CHARACTERS IN THE PARAGRAPH

for (int i=0;para[i]!='\0';i++)
{
    // INCREMENTING THE CHARACTERS WITHOUT THE SPACES.

    if (para[i]!=' ')
    a++;
}


// LOOP FOR COUNTING THE WORDS IN THE PARAGRAPH

for (int i=0;para[i]!='\0';)
{
    // INCREMENTING THE WORDS AFTER THE SPACES.

    if(para[i]==' ')
    b++;

    // DECREMENTING THE SPACES IF THEY ARE MORE THAN ONE
    if(para[i]==' '&&para[i+1]==' ')
    b--;
    i++;

    // CONDITION FOR INCREMENTING THE  WORD
    if(para[i]=='\0' && para[i-1]!=' ')
    b++;

    // CONDITION FOR INCREMENTING THE  WORD 
    if(para[i]=='.'&& para[i]!='\0' && para[i+1]!=' ')
    b++; 

}


// LOOP FOR COUNTING THE SENTENCES IN THE PARAGRAPH

for (int i=0;para[i]!='\0';)
{

    // INCREMENTING THE SENTENCES 
    if(para[i]=='.')
    c++;
    i++;


    // INCREMENTING THE SENTENCES 
    if(para[i]=='\0'&&para[i-1]!='.'&&para[i-1]!=' ')
    c++; 
}

}

For more details about Huffman coding click here

For other assignments and quizzes click here 

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *