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

Search

Solved Problem #CPP#2173    siteicon   siteicon  

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

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

Solution

TC++ #2173

×

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

Alternate (?) :   Related ( ? ) :

char s[100];
Here we declare the array of characters so that whole line can be stored.

cout<<“Type some matter and press enter :”<<endl;
This is prompt to enter the line before the checking loop

cin.getline(s,100);
Here we collect the whole line in one go using the getline() method of cin. Line collection data will be limited to 100 character as the array as been designed like so.

int cnt=0;
Here we set the initial value of the counter.

for(int i=0;s[i]!=”;++i)
This loop runs till the null character is found. A null character is introduced by the getline function as soon as the user presses the enter after the last location of the given content.

if(ispunct(s[i])) ++cnt;
This increases the counter when punctuation mark is found.

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

Notes

  • Using getline() the data collected will actually be maximum array limit – 1. The last character has to be used for or null character if 99 characters are used in an array definition of 100 characters as given in this example.

Common Errors

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


Share

Created: 4-Jan-2018 21:08 | Updated: 4-Jan-2018 21:08|