Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removal of the punctuation from strings given as an input by the user

Am presently reading the book C Primer 5th edition, and this question is asked in the book, question problem being 3.10.
So, basically we have to remove the punctuations if they exist in the string that we would provide it with.
I’ve attempted the question and even I get the successful output when I initialize the string beforehand. Here is my code with strings being initialized before the code execution:

Code:

#include <bits/stdc++.h>
#include <string>
using namespace std;

int main()
{
    string s("he@@,llo world...!!@");

    for(auto &c:s)
    {
       if(ispunct(c))
       {
           cout<<"";
       }
       else
           cout<<c;
       }
       return 0;
 }

This particular code provides with the correct output, i.e. hello world.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now, if I try to use the same code format but with the condition that the user would have to provide the string as an input then the code doesn’t gives the correct output, it just ignores the rest part of the string after the whitespace.

The code that I tried is:

#include <bits/stdc++.h>
#include <string>
using namespace std;

int main()
{
    string s;
    cin>>s;

    for(auto &c:s)
    {
        if(ispunct(c))
        {
            cout<<"";
        }
        else
            cout<<c;
    }
    return 0;
 }

During the execution of the code when I put the string input as he@@,llo world…!!@
The code provides me with the output: hello. The next part of the string after the whitespace gets ignored.

Well, my question is,

Why does this code doesn’t works when the string is taken as the form
of input by the user? And what can I do to make the code work without
any errors?

Edit about suggestion:
The current suggestion provided by one of the community members doesn’t answers my question, as it is not about taking the input from a user and formatting the input in a file, whereas the question asked here is about the removal of the punctuation and characters and printing the rest part of the string when an input is provided by the user.

>Solution :

Standard cin >> only gets the first "word" in a line; words are typically separated by a space, which is why everything after the space after he@@,llo is ignored. What you need to use instead is getline(cin, s) to capture the entire line.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading