473,386 Members | 1,997 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,386 software developers and data experts.

In-place function

Hi There,

Can you please help me write an in-place function in C++ for reversing
a string.

What is an in-place function?

So what is a non-in-place function in that case to reverse a C++
string?

Urgent help needed.

Thanks,

Peter.

Apr 3 '06 #1
7 2403
pe*************@gmail.com wrote:
Can you please help me write an in-place function in C++ for reversing
a string.


#include <string>
#include <algorithm>

void strrev(std::string &s) {
std::reverse(s.begin(), s.end());
}

That kind of homework question is sooo 1980's.

Apr 3 '06 #2
pe*************@gmail.com wrote:
Can you please help me write an in-place function in C++ for reversing
a string.


std::string s = "Hello, world!" ;
std::reverse(s.begin(), s.end()) ;
Apr 3 '06 #3
pe*************@gmail.com wrote:
Hi There,

Can you please help me write an in-place function in C++ for reversing
a string.

What is an in-place function?

So what is a non-in-place function in that case to reverse a C++
string?


I think it's about reversing the string in-place. It means that you don't
create a new string with the same content in reversed order, but rather you
modify the original string by exchanging characters.

Apr 3 '06 #4
In article <11*********************@z34g2000cwc.googlegroups. com>,
pe*************@gmail.com wrote:
Hi There,

Can you please help me write an in-place function in C++ for reversing
a string.


#include <iostream>
#include <string>
// add includes as you think necessary

using namespace std;

void reverse( string& s ) {
// your code here
}

int main() {
string s( "AB" );
reverse( s );
assert( s == "BA" );
cout << "Working\n";
}

Cut and paste the above into your compiler. Add code to the "your code
here" section until 'Working' shows up on your screen when you run it.
Then post back here with what you did.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 3 '06 #5
posted:
Hi There,

Can you please help me write an in-place function in C++ for reversing
a string.

What is an in-place function?

So what is a non-in-place function in that case to reverse a C++
string?

Urgent help needed.

Thanks,

Peter.

This problem is so trivial that I'd challenge myself as to just how
efficient I can make it.

(Yes I realise I'm doing your homework, but this could be a laugh...)

The following code is unchecked and most likely doesn't work, but what the
hey:

#include <cstddef>
#include <cstring>
#include <iostream>
using std::cout; using std::endl;

/* The actual function */
void ReverseString( char* p_beginning, char* p_end )
{
for (;;)
{
std::swap( *p_beginning, *p_end );

switch (p_end++ - p_beginning++)
{
case 1: case 2: return;
}
}
}

/* The function whose argument is an array */
template<std::size_t i>
void ReverseString( char (&p_start)[i] )
{
ReverseString( &p_start[0], &p_start[i - 1] );
}

/* The function which takes a pointer, and also an integral length */
void ReverseString( char* const p_str, std::size_t len )
{
ReverseString( &p_str[0], &p_str[--len] );
}

/* The function which take a pointer */
void ReverseString( char* const p_start )
{
ReverseString( p_start, std::strlen(p_start) );
}

int main()
{
char monkey[] = "monkey";
char cow[] = "cow";
char niagra[] = "niagra";
char stringent[] = "stringent";

ReverseString( monkey ); ReverseString(cow); ReverseString(niagra);

ReverseString( &monkey[0] ); ReverseString( &cow[0] );

cout << monkey << cow << niagra << stringent;
}
-Tomás
Apr 3 '06 #6

Tomás wrote:
posted:
Hi There,

Can you please help me write an in-place function in C++ for reversing
a string.


This problem is so trivial that I'd challenge myself as to just how
efficient I can make it.

(Yes I realise I'm doing your homework, but this could be a laugh...)


How about this one :D
Probably gets bonus points from the teacher (or an instant F :D ):

#include <iostream>
#include <string>

std::string strinv( std::string s )
{
for( unsigned i=0; i<s.size()/2; ++i )
s[s.size()-i-1]^=s[i]^=s[s.size()-i-1]^=s[i];
return s;
}

int main()
{
std::string hello( "Hello World!" );
std::cout << strinv( hello ) << std::endl;
}

Cheers,
Andre

Apr 3 '06 #7
in*****@gmail.com wrote:
Tomás wrote:
posted:
Hi There,

Can you please help me write an in-place function in C++ for reversing
a string.

This problem is so trivial that I'd challenge myself as to just how
efficient I can make it.

(Yes I realise I'm doing your homework, but this could be a laugh...)


How about this one :D
Probably gets bonus points from the teacher (or an instant F :D ):

#include <iostream>
#include <string>

std::string strinv( std::string s )
{
for( unsigned i=0; i<s.size()/2; ++i )
s[s.size()-i-1]^=s[i]^=s[s.size()-i-1]^=s[i];
return s;
}

int main()
{
std::string hello( "Hello World!" );
std::cout << strinv( hello ) << std::endl;
}

Come on, we could come up with something more obfuscated than that if we
really try! :-)
Apr 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Curious Expatriate | last post by:
Hi- I'm completely stumped. I'm trying to write some code that will parse a file and rewrite it with all URLs replaced by something else. For example: if the file looks like this: <b>click...
1
by: JS Bangs | last post by:
I started using PHP's object-oriented stuff a little while ago, which has mostly been a joy. However, I've noticed that they don't seem to echo as I would like. Eg: $this->field = 255;...
5
by: lawrence | last post by:
I've waited 6 weeks for an answer to my other question and still no luck, so let me rephrase the question. I know I can do this: <form method="post" action="$self"> <input type="text"...
1
by: James | last post by:
What is the best way to update a record in a MYSQL DB using a FORM and PHP ? Where ID = $ID ! Any examples or URLS ? Thanks
1
by: phpkid | last post by:
Howdy I've been given conflicting answers about search engines picking up urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3 Do search engines pick up these urls? I've been considering...
1
by: lawrence | last post by:
What is the PHP equivalent of messaging, as in Java?
10
by: lawrence | last post by:
I get the impression that most people who are using objects in their PHP projects are mixing them with procedural code. The design, I think, is one where the procedural code is the client code, and...
2
by: Phillip Wu | last post by:
Hi, I saw a previous post about sending arrays but did not quite understand the answers. The problem is that I would like to pass an entire array as a hidden input field from one php script...
4
by: Matt Schroeder | last post by:
Does anyone know how to count how many rows are in a mysql table? This is what I have, but it doesn't work right: <? $db = mysql_connect("localhost", "username", "password");...
5
by: Martin Lucas-Smith | last post by:
Is there any need to keep the final break in a switch which uses a default at the end? I.e: switch ($data) { case 'foo': # Action break;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.