468,457 Members | 1,563 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Reverse String

From Beginner [ C++ ]:
I am studying a C++ program, using: char, arrays, etc.
The aim of this program is to let it make "Reverse" of any statement
(string) as example:
if the original string is " this is test " ; the output of the program is
reversing this statement to be " tset si siht ".
so the string became reversed.
I am asking: 1- why some of programmers do this ?
2- What's the benefit if they do this ? at any circumstances?
Thank to all.
------------------------------------------------------
Sep 23 '06 #1
9 11647
HELLO $$$ wrote:
I am asking: 1- why some of programmers do this ?
Because someone or some manual tell him to do.
2- What's the benefit if they do this ? at any circumstances?
They have a bigger probability of pass the course if they do.

--
Salu2
Sep 23 '06 #2
I mean what's the practical benefit which reflect on him practically .
----------------------------------------------------------
"Julián Albo" <JU********@terra.eswrote in message
news:45********@x-privat.org...
HELLO $$$ wrote:
>I am asking: 1- why some of programmers do this ?

Because someone or some manual tell him to do.
>2- What's the benefit if they do this ? at any circumstances?

They have a bigger probability of pass the course if they do.

--
Salu2

Sep 23 '06 #3
HELLO $$$ posted:
I mean what's the practical benefit which reflect on him practically.
He/She gets practise at working with algorithms. It's actually quite a
beneficial exercise. I'll give it a quick go without using the Standard
Library for the craic:

template<class T>
inline void SwapObjects(T &a, T&b)
{
T const temp = a;
a = b;
b = temp;
}

template<class T>
inline void ReverseArray(T *pstart, T *pend)
{
do swap(*pstart++,*pend--);
while(pstart < pend);
}

--

Frederick Gotham
Sep 23 '06 #4
"HELLO $$$" writes:
From Beginner [ C++ ]:
I am studying a C++ program, using: char, arrays, etc.
The aim of this program is to let it make "Reverse" of any statement
(string) as example:
if the original string is " this is test " ; the output of the program is
reversing this statement to be " tset si siht ".
so the string became reversed.
I am asking: 1- why some of programmers do this ?
2- What's the benefit if they do this ? at any circumstances?
It's simple a student exercise intended to make you "think like a
programmer". The cases where a real programmer would do such a thing are
probably quite rare.

If you were studying algebra, you would accept those (drill) equations to
solve without knowing what the underlying need is, wouldn't you? A very
similar situation here.
Sep 23 '06 #5
Frederick Gotham posted:
T const temp = a;

Arguably, I should have written that with parentheses rather than an =
symbol.

--

Frederick Gotham
Sep 23 '06 #6
Frederick Gotham wrote :
do swap(*pstart++,*pend--);
Did you mean do SwapObjects(*pstart++,*pend--); ?
Sep 23 '06 #7
Frederick Gotham schrieb:
[why exercises like reversing strings are assigned]
He/She gets practise at working with algorithms. It's actually quite a
beneficial exercise.
This exercise also makes familiar with pointers and pointer arithmetics, or
with arrays and indexes (depends on how it is solved).
I'll give it a quick go without using the Standard
Library for the craic:

template<class T>
inline void SwapObjects(T &a, T&b)
{
T const temp = a;
a = b;
b = temp;
}

template<class T>
inline void ReverseArray(T *pstart, T *pend)
{
do swap(*pstart++,*pend--);
while(pstart < pend);
}
You should document, what pstart and pend points to (including or excluding
start and end), because your code accesses *pend, which is a no-no! for
standard library style iterators that points to the end.

begin/end iterators pairs are specified by 'begin' referring to the first
element and 'end' referring to one past the last element in the sequence.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 23 '06 #8
loufoque posted:
> do swap(*pstart++,*pend--);

Did you mean do SwapObjects(*pstart++,*pend--); ?

Yes, I should've written SwapObjects.

--

Frederick Gotham
Sep 24 '06 #9
Thomas J. Gritzan posted:
You should document, what pstart and pend points to (including or excluding
start and end), because your code accesses *pend, which is a no-no! for
standard library style iterators that points to the end.

begin/end iterators pairs are specified by 'begin' referring to the first
element and 'end' referring to one past the last element in the sequence.
I use "p_end" to refer to the last element, and "p_over" to refer to one past
the last. If I were to write intructions for usage of the function though,
I'd certainly indicate what pstart and pend refer to.

--

Frederick Gotham
Sep 24 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

59 posts views Thread by Raymond Hettinger | last post: by
8 posts views Thread by Jim Langston | last post: by
20 posts views Thread by sahukar praveen | last post: by
24 posts views Thread by Sathyaish | last post: by
47 posts views Thread by sudharsan | last post: by
15 posts views Thread by rajash | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.