In-place function

  • Thread starter peter.mcclymont
  • Start date
P

peter.mcclymont

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

Pete C

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

Alan Johnson

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()) ;
 
R

Rolf Magnus

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

Daniel T.

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

Tomás

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) )
{
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
 
I

int2str

Tomás said:
posted:


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^=s[s.size()-i-1]^=s;
return s;
}

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

Cheers,
Andre
 
R

red floyd

Tomás said:
posted:

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^=s[s.size()-i-1]^=s;
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! :)
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top