Optimized code for finding string length

S

SSG

Hai All!

I need the optimized code for finding the string length. i dont want to
use strlen function.........

can anyone know reply........

By
S.S.G
 
E

Erik de Castro Lopo

SSG said:
Hai All!

I need the optimized code for finding the string length. i dont want to
use strlen function.........

Why not? Think you can do better?

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
Moore's Law: hardware speed doubles every 18 months
Gates' Law: software speed halves every 18 months
 
M

Mark

SSG said:
Hai All!

I need the optimized code for finding the string length. i dont want to
use strlen function.........
Why not? Homework problem?
Do your own homework Eshita.
can anyone know reply........
By
S.S.G

I'm sure the people at (e-mail address removed) know the answers to your
questions... ask them.
 
C

chellappa

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
long value;
char src[]="71";
char *err;
errno = 0;
value = strtol(src, &err, 8);
perror("strtol");
printf("%ld %ld\n", value, LONG_MAX);
printf("%d\n", *err);
return 0;
}
 
A

akarl

SSG said:
Hai All!

I need the optimized code for finding the string length. i dont want to
use strlen function.........

can anyone know reply........

By
S.S.G

I doubt the strlen function can be optimized. After all we have to go
through the character array until we find 0. Try to optimize the
function that needs the string length. Are you sure you really need it
in advance?
 
W

websnarf

SSG said:
I need the optimized code for finding the string length. i dont want to
use strlen function.........

The easiest way to optimize knowing the length of strings, is to simply
have them procomputed and lying around along with the string. See:
http://bstring.sf.net/ for an example of this. The Better String
Library generally beats the C-library in terms of performance precisely
because it never performs strlen()'s or implicitely equivalent
computations (except when converting from legacy char * strings.)

If you must stick with char * strings, then see Example #5 on my
assembly examples page:
http://www.azillionmonkeys.com/qed/asmexample.html and look for the
last code snippet in that example. Its C code that will work on most
systems, and beats most compilers (though I am told the Sun compiler
implements essentially this trick already, thus having the same
performance).
 
K

Keith Thompson

chellappa said:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
long value;
char src[]="71";
char *err;
errno = 0;
value = strtol(src, &err, 8);
perror("strtol");
printf("%ld %ld\n", value, LONG_MAX);
printf("%d\n", *err);
return 0;
}

"err" is a poor name for something to be used for the second argument
in a call to strtol().

You call perror() without checking whether there was actually an
error.

The program interprets the string "71" as an octal number. I suppose
that might be a useful thing to do in some circumstances, but I'm at a
loss to understand the point.

The article to which you're responding asked about finding the length
of a string. Your program doesn't do that.

As usual, you've posted without providing any context from the
previous article.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top