length of strings w. newlines in them 
Author Message
 length of strings w. newlines in them

Hello,

My app. is an internet app. that often has to deal  with a lot of
strings that can have newlines, or other odd ascii characters,  before
the last newline at the end of the string.  

Anyway, I need to calculate the entire length of these strings so I
can correctly parse them.  Of course the standard strlen won't work
for this so I've ended up writing the simple function at the bottom
here.

My question is, is there a better way to do this?  It seems like this
must be a fairly common thing to have to deal with.

Once you throw newlines into the mix C string functions seem to become
very ineffectual.  At least since I'm used to the power a  language
such as  Perl has with strings...

Thanks for any insight.!

static unsigned int FullStrLen(char *pString)
{
        unsigned int i = 0;

        while( *(pString++) )
                i++;

        return i;      

Quote:
}



Thu, 08 Apr 2004 14:33:21 GMT  
 length of strings w. newlines in them

Quote:

>Hello,

>My app. is an internet app. that often has to deal  with a lot of
>strings that can have newlines, or other odd ascii characters,  before
>the last newline at the end of the string.

>Anyway, I need to calculate the entire length of these strings so I
>can correctly parse them.  Of course the standard strlen won't work
>for this

strlen() will work just fine.  It does not apply
any significance to newline characters, only to '\0'.

char msg[] = "Hello\nworld\n");
printf("%lu\n", strlen(msg)); /* prints 12 */

Your example code is essentially what strlen() does.

-Mike



Thu, 08 Apr 2004 15:30:57 GMT  
 length of strings w. newlines in them
You could do this:

int n = memchr(buff, 0, MAX_LEN) - buff;

That assumes, of course, that the string ends in \0 and you know
the maximum size of your buffer (which you should).  The above doesn't
do any error checking, of course, but I think it's more or less
what you do in your code.

Quote:

> Hello,
> My app. is an internet app. that often has to deal  with a lot of
> strings that can have newlines, or other odd ascii characters,  before
> the last newline at the end of the string.  
> Anyway, I need to calculate the entire length of these strings so I
> can correctly parse them.  Of course the standard strlen won't work
> for this so I've ended up writing the simple function at the bottom
> here.
> My question is, is there a better way to do this?  It seems like this
> must be a fairly common thing to have to deal with.
> Once you throw newlines into the mix C string functions seem to become
> very ineffectual.  At least since I'm used to the power a  language
> such as  Perl has with strings...
> Thanks for any insight.!

> static unsigned int FullStrLen(char *pString)
> {
>    unsigned int i = 0;
>    while( *(pString++) )
>            i++;

>    return i;      
> }

--
Tyler Larson  |  http://www.tlarson.com  |  news(a)tlarson.com
If I cannot bend Heaven, I shall move Hell.
                -- Publius Vergilius Maro (Virgil)


Sun, 11 Apr 2004 14:24:40 GMT  
 length of strings w. newlines in them

Quote:

> You could do this:

> int n = memchr(buff, 0, MAX_LEN) - buff;

> That assumes, of course, that the string ends in \0 and you know
> the maximum size of your buffer (which you should).  The above doesn't
> do any error checking, of course, but I think it's more or less
> what you do in your code.

What's wrong with using strlen ?

--
 pete



Sun, 11 Apr 2004 21:22:39 GMT  
 length of strings w. newlines in them

Quote:

> You could do this:

> int n = memchr(buff, 0, MAX_LEN) - buff;

> That assumes, of course, that the string ends in \0

It does. By definition. If it doesn't, it isn't a string.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sun, 11 Apr 2004 23:33:43 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. strings: length of strings

2. How do you get a newline in a textbox and string in C#

3. insert newlines into long string

4. string newline replace

5. stripping newlines from strings

6. newlines in string constants

7. building a string with newline characters

8. newline in string

9. Adding a newline to a string

10. Newline in commandline string

11. formatting long strings using sprintf() without including tabs or newlines

12. Remove newlines from string

 

 
Powered by phpBB® Forum Software