[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

check if line is whitespace

puzzlecracker

9/3/2008 5:22:00 PM

What is the quickest way to check that the following:

const line[127]; only contains whitespace, in which case to ignore it.

something along these lines:

isspacedLine(line);

Thanks
15 Answers

Zeppe

9/3/2008 5:33:00 PM

0

puzzlecracker wrote:
> What is the quickest way to check that the following:
>
> const line[127]; only contains whitespace, in which case to ignore it.
>
> something along these lines:
>
> isspacedLine(line);
>

const line[127];

doesn't mean anything in c++. Apart from that, if line is an array of
char, I'm pretty much sure that somebody with "puzzlecracker" as
nickname will be more than able to solve it ;)

Best wishes,

Zeppe

utab

9/3/2008 5:44:00 PM

0

On Wed, 03 Sep 2008 10:21:58 -0700, puzzlecracker wrote:

> What is the quickest way to check that the following:
>
> const line[127]; only contains whitespace, in which case to ignore it.
>
> something along these lines:
>
> isspacedLine(line);
>
> Thanks

supposing you are trying to read some input lines of 127 char wide... I
also think that "const char line[127]" is mentioned...

Read with getline() and check if empty with empty() function of the
string class.

// string::empty
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string content;
string line;
cout << "Please introduce a text. Enter an empty line to finish:\n";
do {
getline(cin,line);
content += line + '\n';
} while (!line.empty());
cout << "The text you introduced was:\n" << content;
return 0;
}

HTH,

utab

9/3/2008 5:46:00 PM

0

On Wed, 03 Sep 2008 19:43:50 +0200, utab wrote:

> On Wed, 03 Sep 2008 10:21:58 -0700, puzzlecracker wrote:
>
>> What is the quickest way to check that the following:
>>
>> const line[127]; only contains whitespace, in which case to ignore it.
>>
>> something along these lines:
>>
>> isspacedLine(line);
>>
>> Thanks
>
> supposing you are trying to read some input lines of 127 char wide... I
> also think that "const char line[127]" is mentioned...
>
Of course with getline const char ... is wrong.

Darío

9/3/2008 5:49:00 PM

0

On Sep 3, 2:21 pm, puzzlecracker <ironsel2...@gmail.com> wrote:
> What is the quickest way to check that the following:
>
> const line[127]; only contains whitespace, in which case to ignore it.
>
> something along these lines:
>
> isspacedLine(line);
>
> Thanks

bool isLineSpaced(const char line[127])
{
int i = 0;
for(; i<127 && line[i++] == ' '; );
return i==127;
}

puzzlecracker

9/3/2008 6:20:00 PM

0

Guys, yeah, I wrote something similar to yours suggestions:

if( (line[strlen(line) -1] == '\n') )
line[strlen(line) -1] = '\0';

//ignore whitespace lines
unsigned int i;
for(i=0; line[i]!='\0' && isspace(line[i]);i++)
;
if(i==strlen(line))
continue;

utab

9/3/2008 8:39:00 PM

0

On Wed, 03 Sep 2008 11:19:30 -0700, puzzlecracker wrote:

> Guys, yeah, I wrote something similar to yours suggestions:
>
> if( (line[strlen(line) -1] == '\n') )
> line[strlen(line) -1] = '\0';
>
> //ignore whitespace lines
> unsigned int i;
> for(i=0; line[i]!='\0' && isspace(line[i]);i++)
> ;
> if(i==strlen(line))
> continue

Why not use strings instead ;)

Sam

9/3/2008 11:04:00 PM

0

Darío writes:

> On Sep 3, 2:21 pm, puzzlecracker <ironsel2...@gmail.com> wrote:
>> What is the quickest way to check that the following:
>>
>> const line[127]; only contains whitespace, in which case to ignore it.
>>
>> something along these lines:
>>
>> isspacedLine(line);
>>
>> Thanks
>
> bool isLineSpaced(const char line[127])
> {
> int i = 0;
> for(; i<127 && line[i++] == ' '; );
> return i==127;
> }

That's C, not C++.

The C++ solution would be:

#include <algorithm>
#include <cctype>
#include <functional>
#include <vector>

bool isLineSpaced(const std::vector<char> &line)
{
return std::find_if(line.begin(), line.end(),
std::not1(std::ptr_fun(isspace))) == line.end();
}


James Kanze

9/4/2008 8:15:00 AM

0

On Sep 3, 7:21 pm, puzzlecracker <ironsel2...@gmail.com> wrote:
> What is the quickest way to check that the following:

> const line[127]; only contains whitespace, in which case to ignore it.

You mean std::string line, don't you. The above isn't a legal
C++ declaration.

> something along these lines:

> isspacedLine(line);

Well, the standard library already has direct support for this,
but it's interface isn't the most friendly. But something like
the following should do the trick:

bool
isOnlySpaces(
std::string const& line,
std::locale const& locale = std::locale() )
{
return std::use_facet< std::ctype< char > >( locale )
.scan_not( std::ctype_base::space,
line.data(), line.data() + line.size() )
== line.data() + line.size() ;
}

(If you're forced to use arrays of char, instead of string, this
solution still works perfectly well.)

More generally, however, I tend to use regular expressions in
such cases. If the line matches "^[:space:]*$", ignore it.
With a good implementation of regular expressions (which uses a
DFA if the expression contains no extensions), this can be just
as fast as the above, if not faster. (Just make sure you only
construct the regular expression once, and not every time you
call the function.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

9/4/2008 9:42:00 AM

0

On Sep 4, 1:03 am, Sam <s...@email-scan.com> wrote:
> Darío writes:
> > On Sep 3, 2:21 pm, puzzlecracker <ironsel2...@gmail.com> wrote:
> >> What is the quickest way to check that the following:

> >> const line[127]; only contains whitespace, in which case to ignore it.

> >> something along these lines:

> >> isspacedLine(line);

> > bool isLineSpaced(const char line[127])
> > {
> > int i = 0;
> > for(; i<127 && line[i++] == ' '; );
> > return i==127;
> > }

> That's C, not C++.

Well, it's also C++, albeit not idiomatic or good C++.

> The C++ solution would be:

> #include <algorithm>
> #include <cctype>

The C++ solution would use <locale>, and not <cctype>:-). (With
subsequent changes in the code, of course.)

> #include <functional>
> #include <vector>

> bool isLineSpaced(const std::vector<char> &line)
> {
> return std::find_if(line.begin(), line.end(),
> std::not1(std::ptr_fun(isspace))) == line..end();
> }

Which is fine, except that it has undefined behavior. What you
probably meant was somthing like:

struct NotIsSpace
{
bool operator()( char ch ) const
{
return ! std::isspace(
static_cast< unsigned char >( ch ) ) ;
}
} ;

bool
isEmptyLine(
std::string const& line )
{
return std::find_if( line.begin(), line.end(), NotIsSpace() )
== line.end() ;
}

(You cannot call the version of isspace in <cctype> with a char
without risking undefined behavior.)

Still, a quick benchmark shows that something like:

myCtype.scan_not( std::ctype_base::space,
myData.data(),
myData.data() + myData.size() )
== myData.data() + myData.size() ;

, with myCtype initialized with "std::use_facet< std::ctype<
char > >( std::locale()" is roughly five times faster (at least
on one system: g++ 4.1 under Linux on an Intel). And it's
certainly more idiotic^H^H^Hmatic with regards to C++.

(FWIW, using a full regular expression was only about three
times slower than your solution. And is a lot more powerful.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nick Keighley

9/4/2008 9:47:00 AM

0

On 3 Sep, 18:21, puzzlecracker <ironsel2...@gmail.com> wrote:

> What is the quickest way to check that the following:
>
> const line[127]; only contains whitespace, in which case to ignore it.
>
> something along these lines:
>
> isspacedLine(line);

is a C solution any good?

#include <cstring>

bool isspacedLine (const char* line)
{
size_t i = strspn (line, " \t\f\n");
return line[i] = '\0';
}

--
Nick Keighley