473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3680
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
4
by: Porthos | last post by:
Hi All, I'm trying to find the minimum value of a set of data (see below). I want to compare the lengths of these attribute values and display the lowest one. This would be simple if I could...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
24
by: shafique | last post by:
Hello, Can anybody have idea as how to find it there exist consective one's in a word/dword using bitwise operatiors in C language. Obviously this can be done by shifting and looping one by...
6
by: Tarun | last post by:
Hi All, I need to find a particular substring in a binary string(some text appended & prepended to binary data). I cant use strstr since it terminates on receiving '\0'which can be there in...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
4
by: Glenn | last post by:
OK, I've looked up this message but am not finding how to get rid of it: "Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.