470,612 Members | 2,178 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,612 developers. It's quick & easy.

Removing Spaces and Punctuations From a Line

I was wondering if someone can show me how to remove spacing and punctuations from a line of input using character functions(such as is punct., is space) I also want to use a for loop to do it. I just don't know where to start on this. I think I should use if statements stating if(i is space) then remove it but im not sure how too do that.

Thanks,
Nov 19 '06 #1
6 2011
sivadhas2006
142 100+
Hi,

Can you post your program whatever you have tried to do that?

Regards,
M.Sivadhas.
Nov 20 '06 #2
yeah no problem.
for(i=0; i<str.size(); ++i)
{
if(i is punct.)
str.erase(i,1)
if(i is space)
str.erase(i,1)
}
Nov 20 '06 #3
Banfa
9,065 Expert Mod 8TB
That is not that bad a start, however you only want to increment i if you haven't erased a character
Nov 20 '06 #4
I think I am going to create a temporary string:

string CleanString (string old, int& numspaces, int& numpunct)
{
string newold;
for(int i=0; i<old.size(); ++i)
{

if(!isspace(old[i]) || !ispunct(old[i]))
newold = newold+old[i];
}
Nov 20 '06 #5
Banfa
9,065 Expert Mod 8TB
I think I am going to create a temporary string:
Not a bad plan but

1. initialise newold to ""

2. newold = newold+old[i]; would be better written

newold += old[i];

as it doesn't envolve the creation of a temporary string for the expression.
Nov 20 '06 #6
[quote=Banfa]Not a bad plan but

1. initialise newold to ""

2. newold = newold+old[i]; would be better written

newold += old[i];

as it doesn't envolve the creation of a temporary string for the expression.[/QUOTE

Thanks for the help
Nov 20 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Andy Visniewski | last post: by
11 posts views Thread by gopal srinivasan | last post: by
12 posts views Thread by Magix | last post: by
3 posts views Thread by lino | last post: by
2 posts views Thread by vijayk | last post: by
9 posts views Thread by David Pratt | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.