Count punctuation in user given text stream (using get) – CSKC
Sign In

Search

Solved Problem #CPP#2167    siteicon   siteicon  

Problem Statement - Count punctuation in user given text stream (using get)

Write a program to find punctuation in the user given text data. Use the function ispunct() for finding punctuation and use get() function of cin method to collect the user given text matter in a loop.

Solution

TC++ #2167

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

char nl=’n’;
Here we are initialising a char variable nl with n which is the escape sequence for new line character. This will help us find when the user terminates his entry by pressing enter.

char c;Β int cnt=0;
These variables shall collect suitable data.

cout<<“Type some matter and press enter :”<<endl;
This is prompt before the loop starts.

do
We use do while loop here as we want to await for some entry before the newline check is performed.
{
cin.get(c);
This way we take one character at a time. Loop continues till the user pressed the enter key (new line) so with this line done repeatedly we can collect a very long matter.

if(ispunct(c)) cnt++;
This line checks the user input character if it is a punctuation mark and increase a count which was earlier set to 0 initially.

}while(c!=nl);
At loop exit we check if the newline character has been pressed and we exit the loop only when new line match is found.

cout<<“Number of punctuation marks = “<<cnt<<endl;
This line prints the count.

Common Errors

  • Forgetting to initialise the count with 0 can give undefined results.


Share

Created: 4-Jan-2018 20:40 | Updated: 4-Jan-2018 20:58|