Convert tab to spaces?

N

none

I am trying to use this function:

std::string RemoveMultipleWhiteSpaces( std::string s ){
std::string search = " "; // this is 2 spaces
size_t index;
while( (index = s.find(search)) != std::string::npos ) {
s.erase(index,1);
}
return s;
}


to convert duplicate blanks or tabs in a string to single blanks, eg.:

a b cc d

shold be:
a b cc d


But if my input string contains a tab it does not work. How do I modify the above to work correctly
with tabs in a string?
 
S

Sjouke Burry

none said:
I am trying to use this function:

std::string RemoveMultipleWhiteSpaces( std::string s ){
std::string search = " "; // this is 2 spaces
size_t index;
while( (index = s.find(search)) != std::string::npos ) {
s.erase(index,1);
}
return s;
}


to convert duplicate blanks or tabs in a string to single blanks, eg.:

a b cc d

shold be:
a b cc d


But if my input string contains a tab it does not work. How do I modify the above to work correctly
with tabs in a string?
First replace the tabs with space chars.
Or treat spaces and tabs equally as whitespace.
 
A

Alf P. Steinbach

* none:
I am trying to use this function:

std::string RemoveMultipleWhiteSpaces( std::string s ){
std::string search = " "; // this is 2 spaces
size_t index;
while( (index = s.find(search)) != std::string::npos ) {
s.erase(index,1);
}
return s;
}


to convert duplicate blanks or tabs in a string to single blanks, eg.:

a b cc d

shold be:
a b cc d


But if my input string contains a tab it does not work. How do I modify
the above to work correctly with tabs in a string?

Iterate trough the characters of s, copying each character to a result string,
except that when both the previous and this character are whitespace, don't copy.


Cheers & hth.,

- Alf
 
N

none

Sjouke said:
First replace the tabs with space chars.
Or treat spaces and tabs equally as whitespace.

In have now tried to change:

std::string search = " "; // this is 2 spaces

into:



std::string search = " \t";


But then I get tabs inserted instead. Any ideas?
 
S

Sjouke Burry

none said:
In have now tried to change:

std::string search = " "; // this is 2 spaces

into:



std::string search = " \t";


But then I get tabs inserted instead. Any ideas?
replace (index = s.find(search) by

(index = s.find(search1) || (index = s.find(search2)

and define search1 and search2 properly?
Btw, your search string contains 3 chars.
 
N

none

Sjouke said:
replace (index = s.find(search) by

(index = s.find(search1) || (index = s.find(search2)

and define search1 and search2 properly?
Btw, your search string contains 3 chars.


Hm that just creates an infinite loop:

std::string RemoveMultipleWhiteSpaces( std::string s ){
std::string search1 = " "; // this is 2 spaces
std::string search2 = "\t"; // this is 2 spaces
size_t index;


while( (index = s.find(search1) || (index = s.find(search2))) != std::string::npos ) {
// remove 1 character from the string at index
s.erase(index,1);
}
return s;
}
 
J

James Kanze

[...]
Hm that just creates an infinite loop:
std::string RemoveMultipleWhiteSpaces( std::string s ){
std::string search1 = " "; // this is 2 spaces
std::string search2 = "\t"; // this is 2 spaces
size_t index;
while( (index = s.find(search1) || (index = s.find(search2))) != std::string::npos ) {
// remove 1 character from the string at index
s.erase(index,1);
}
return s;
}

If you're generating a new string, a much better approach might
be:

typedef std::string::const_iterator TextIterator;

std::string
compressWhiteSpace(
TextIterator begin,
TextIterator end)
{
std::string result;
TextIterator current = std::find_if(begin, end, IsNotSpace());
TextIterator next = std::find_if(current, end, IsSpace());
while (next != end) {
result.append(current, next);
current = std::find_if(next + 1, end, IsNotSpace());
if ( current == end ) {
next = current;
} else {
result += ' ';
next = std::find_if(current, end, IsSpace());
}
}
if ( current != end) {
std::copy(current, end, dest);
}
}

If you're doing any text processing whatsoever, you already have
the predicates IsSpace and IsNotSpace, so you might as well use
them. (And of course, they'll use either the standard isspace
function in <ctype.h> or an instantiation of std::ctype from
<locale>, rather than looking up characters in a string.)
 
J

James Kanze

Sjouke said:
none wrote:
[...]

Hm that just creates an infinite loop:
std::string RemoveMultipleWhiteSpaces( std::string s ){
std::string search1 = " "; // this is 2 spaces
std::string search2 = "\t"; // this is 2 spaces
size_t index;
while( (index = s.find(search1) || (index = s.find(search2))) != std::string::npos ) {
// remove 1 character from the string at index
s.erase(index,1);
}
return s;
}

If you're generating a new string, a much better approach might
be:

typedef std::string::const_iterator TextIterator;

std::string
compressWhiteSpace(
TextIterator begin,
TextIterator end)
{
std::string result;
TextIterator current = std::find_if(begin, end, IsNotSpace());
TextIterator next = std::find_if(current, end, IsSpace());
while (next != end) {
result.append(current, next);
current = std::find_if(next + 1, end, IsNotSpace());
if ( current == end ) {
next = current;
} else {
result += ' ';
next = std::find_if(current, end, IsSpace());
}
}
if ( current != end) {
std::copy(current, end, dest);

Sorry: that should be:
result.append(current, end);
(I'd started with something more complex, using an output
iterator to write the results. Then I remembered that there's
such a thing as being over-generic.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top