445,804 Members | 1,666 Online
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

# number of characters in a string

 P: n/a 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 Nov 13 '05 #1
6 Replies

 P: n/a berthelot samuel wrote: Is there a function in C that computes the number of characters in a string. Yes, it's strlen(). Don't forget #include . 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 Nov 13 '05 #2

 P: n/a "Sidney Cadot" schrieb im Newsbeitrag news:bp**********@news.tudelft.nl... berthelot samuel wrote: Is there a function in C that computes the number of characters in a string. Yes, it's strlen(). Don't forget #include . 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. Can it be, that the OP has wide char strings? In this case wcslen() is what he/she needs Just a wild guess Robert Nov 13 '05 #3

 P: n/a sa**************@voila.fr (berthelot samuel) wrote: 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 int strGraphCount( const char *s ) { int res = 0; for( ; *s; ++s ) if ( isgraph( *s ) ) ++res; return res; } HTH Regards -- Irrwahn (ir*******@freenet.de) Nov 13 '05 #4

 P: n/a Robert Stankowic wrote: "Sidney Cadot" schrieb im Newsbeitrag news:bp**********@news.tudelft.nl...berthelot samuel wrote:Is there a function in C that computes the number of characters in astring.Yes, it's strlen(). Don't forget #include .I want to be able to compute the number of chars in the string '1 3 45' 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. 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 Nov 13 '05 #5

 P: n/a Irrwahn Grausewitz wrote in message news:. .. sa**************@voila.fr (berthelot samuel) wrote: 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 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 ) ) ++res; return res; } HTH Regards -- Peter Nov 13 '05 #6

 P: n/a ai***@acay.com.au (Peter Nilsson) wrote: Irrwahn Grausewitz wrote: #include int strGraphCount( const char *s ) { int res = 0; I'd go with size_t, rather than int. That'd be better design, yes. for( ; *s; ++s ) if ( isgraph( *s ) ) if ( isgraph( (unsigned char) *s ) ) Uck, that was a real mistake, thanks for correction. ++res; return res; } Regards -- Irrwahn (ir*******@freenet.de) Nov 13 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.