469,645 Members | 1,653 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Optimized code for finding string length

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

Nov 15 '05 #1
6 3520
SSG wrote:

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 no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
Moore's Law: hardware speed doubles every 18 months
Gates' Law: software speed halves every 18 months
Nov 15 '05 #2
"SSG" <ss****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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 so*******@icreon.com know the answers to your
questions... ask them.
Nov 15 '05 #3
#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;
}

Nov 15 '05 #4
SSG wrote:
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?

Nov 15 '05 #5
SSG wrote:
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).

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 15 '05 #6
"chellappa" <N.*********@gmail.com> writes:
#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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bryan Olson | last post: by
6 posts views Thread by Tarun | last post: by
17 posts views Thread by Mark | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.