Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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.
Check character group as alpha, digit, control, punctuation, lower, upper etc.
#include
using namespace std;
int main()
{
char nl='n';
char c;
int cnt=0;
cout<<"Type some matter and press enter :"<Copy-->
×
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
The code has been copied automatically for you in the clipboard. After that open any of the service and press cntrl-v to paste in the given code area. Needs a bit of tweaking as per the link.
Related codes are those codes where similar programming concepts are in use. They are different from alternate codes in the sense that they may not have same purpose and results.
:
Run Output
Type some matter and press enter :
Hello! how are you?
Number of punctuation marks = 2
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.