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.
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.