Re: reversing a string - newbie question
From: Martin Dickopp (expires-2004-04-30_at_zero-based.org)
Date: 03/02/04
- Next message: Eric Sosman: "Re: Fast sincos routine"
- Previous message: Sathyaish: "Re: Please tell me if I've written this correctly"
- In reply to: Mark Henning: "Re: reversing a string - newbie question"
- Next in thread: CBFalconer: "Re: reversing a string - newbie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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. \_/
- Next message: Eric Sosman: "Re: Fast sincos routine"
- Previous message: Sathyaish: "Re: Please tell me if I've written this correctly"
- In reply to: Mark Henning: "Re: reversing a string - newbie question"
- Next in thread: CBFalconer: "Re: reversing a string - newbie question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|