473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 11878
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
8
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse,...
21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
24
by: Sathyaish | last post by:
This one question is asked modally in most Microsoft interviews. I started to contemplate various implementations for it. This was what I got. #include <stdio.h> #include <stdlib.h> #include...
47
by: sudharsan | last post by:
could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
15
by: rajash | last post by:
Thanks for the additional comments. Here is a solution to an exercise I had problems with. I still don't think it's really what's wanted as it uses a "state variable" n - but I can't see how to...
1
by: sunnyluthra1 | last post by:
Hi, I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long. I successfully able to did that. Here is the code:...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.