Reverse String

H

HELLO $$$

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.
------------------------------------------------------
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

HELLO said:
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.
 
H

HELLO $$$

I mean what's the practical benefit which reflect on him practically .
----------------------------------------------------------
 
F

Frederick Gotham

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);
}
 
O

osmium

HELLO $$$ said:
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.
 
T

Thomas J. Gritzan

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.
 
F

Frederick Gotham

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top