number of characters in a string

B

berthelot samuel

Hi,
Is there a function in C that computes the number of characters in a
string.
I want to be able to compute the number of chars in the string '1 3 4
5' by suppressing the ' ', so length should be 4 instead of 7...
Thanx
Sam
 
S

Sidney Cadot

berthelot said:
Is there a function in C that computes the number of characters in a
string.

Yes said:
I want to be able to compute the number of chars in the string '1 3 4
5' by suppressing the ' ', so length should be 4 instead of 7...

What is the exact text of the problem you have to solve for your course?
My guess is that you are taking the wrong approach.

Best regards,

Sidney
 
R

Robert Stankowic

Sidney Cadot said:
What is the exact text of the problem you have to solve for your course?
My guess is that you are taking the wrong approach.

Can it be, that the OP has wide char strings?
In this case wcslen() is what he/she needs

Just a wild guess
Robert
 
I

Irrwahn Grausewitz

Hi,
Is there a function in C that computes the number of characters in a
string.
I want to be able to compute the number of chars in the string '1 3 4
5' by suppressing the ' ', so length should be 4 instead of 7...

There's no such standard function, but it's easy to write one
yourself. Just lookup the various is...() functions declared
in ctype.h and do something like:

#include <ctype.h>

int strGraphCount( const char *s )
{
int res = 0;
for( ; *s; ++s )
if ( isgraph( *s ) )
++res;
return res;
}

HTH
Regards
 
S

Sidney Cadot

Robert said:
Can it be, that the OP has wide char strings?
In this case wcslen() is what he/she needs

Perhaps (that would be a problem for the course in advanced C
programming, I presume).

The OP's question smells like homework - I guess that's what I am trying
to say.

Regards,

Sidney
 
P

Peter Nilsson

Irrwahn Grausewitz said:
There's no such standard function, but it's easy to write one
yourself. Just lookup the various is...() functions declared
in ctype.h and do something like:

#include <ctype.h>

int strGraphCount( const char *s )
{
int res = 0;

I'd go with size_t, rather than int.
for( ; *s; ++s )
if ( isgraph( *s ) )

if ( isgraph( (unsigned char) *s ) )
 

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,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top