By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,985 Members | 2,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,985 IT Pros & Developers. It's quick & easy.

Reverse String

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
Frederick Gotham wrote :
do swap(*pstart++,*pend--);
Did you mean do SwapObjects(*pstart++,*pend--); ?
Sep 23 '06 #7

P: n/a
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

P: n/a
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

P: n/a
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.