Re: reversing a string - newbie question

From: Martin Dickopp (expires-2004-04-30_at_zero-based.org)
Date: 03/02/04


Date: Tue, 02 Mar 2004 15:58:09 +0100


"Mark Henning" <mahenning@btopenworld.com> writes:

>> More efficient than what? You cannot compare the efficiency of your loop
>> to the other one, as they do different things. Yours iterates through
>> the string in forward direction, while the other iterates through the
>> string in reverse direction.
>
> You misunderstand me.

Apparently I did. Sorry.

> I was not comparing it to the loop of the OP, since my method will work only
> when incrementing i, as you point out. Seeing strlen() used in a for loop to
> pass through a string prompted me to ask the question, off-topic as it is.
>
> I will rephrase the question for your benefit.
>
> Are there any benefits to computing the string length using strlen() in
> order to loop through a string rather than merely incrementing the pointer
> until the NUL character is found?

Since `strlen' has to iterate through the string to find its length, it
would most likely be wasteful to call `strlen' if you want to use the
result to forward iterate through the string. Effectively, you would
iterate through it twice instead of once.

While the OP could not avoid to iterate through the string twice, you
can and should avoid it if you only want to iterate in forward
direction.

Martin

-- 
   ,--.    Martin Dickopp, Dresden, Germany                 ,= ,-_-. =.
  / ,- )   http://www.zero-based.org/                      ((_/)o o(\_))
  \ `-'                                                     `-'(. .)`-'
   `-.     Debian, a variant of the GNU operating system.       \_/


Relevant Pages

  • Re: Is it possible
    ... The object browser can list all ... Dim FunctionName As String ... you could iterate through the passed ... Variant array to retrieve the individual elements (which now ...
    (comp.lang.basic.visual.misc)
  • JSP to MySQL Connector/J problem
    ... handshaking is good and I iterate threw the result set with the next ... I manually add another row from the mysql> prompt and I ... String formattedDate = fmt.format; ... ResultSetMetaData rsmd = null; ...
    (comp.programming)
  • Re: Record seperator
    ... into a string. ... It's always annoyed me a little that while it's easy to iterate over the ... it's more complicated to iterate over a file character ...
    (comp.lang.python)
  • Re: Properties for a custom class (not a collection)
    ... Public Property Get properties(key As String) As String ... Dim pc As clsPropetyCollection ... If you want to iterate the collection ... You write an iterator class or provide ...
    (comp.databases.ms-access)
  • Re: C or C++
    ... people who know C tend to avoid using the C++ standard ... C string handling. ... programmer. ... using the preprocessor for integer constants should be ...
    (comp.os.linux.development.apps)