Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reverse String

Reply
Thread Tools

Reverse String

 
 
HELLO $$$
Guest
Posts: n/a
 
      09-23-2006
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.
------------------------------------------------------


 
Reply With Quote
 
 
 
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      09-23-2006
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
 
Reply With Quote
 
 
 
 
HELLO $$$
Guest
Posts: n/a
 
      09-23-2006
I mean what's the practical benefit which reflect on him practically .
----------------------------------------------------------
"Julián Albo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-23-2006
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
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      09-23-2006
"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.


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-23-2006
Frederick Gotham posted:

> T const temp = a;



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

--

Frederick Gotham
 
Reply With Quote
 
loufoque
Guest
Posts: n/a
 
      09-23-2006
Frederick Gotham wrote :

> do swap(*pstart++,*pend--);


Did you mean do SwapObjects(*pstart++,*pend--); ?
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      09-23-2006
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
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-24-2006
loufoque posted:

>> do swap(*pstart++,*pend--);

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



Yes, I should've written SwapObjects.

--

Frederick Gotham
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-24-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reverse string, how to print string and not decimals? ssecorp C Programming 47 08-08-2008 06:48 PM
Accidental Reverse order String display Roedy Green Java 9 08-11-2005 01:30 PM
String Reverse Question Rakesh C Programming 45 04-20-2004 02:06 AM
Re: Reverse function of Server.MapPath(string file)? Curt_C [MVP] ASP .Net 0 01-22-2004 05:36 PM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM



Advertisments