I am an active software engineer.
Wednesday, May 15, 2013
Remove space from string C++
Write a function that removes extra spaces from a string and leaves only one correct space.
void
removespace(
string
&s){
int
count = 0;
string
buffer;
for
(
int
i = 0; i < s.length(); i++){
if
(s[i] != ' ') {
count = 0;
buffer.push_back(s[i]);
}
else
{
if
(count == 0) {
count++;
buffer.push_back(' ');
}
}
}
s = buffer;
}
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment