Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > In-place function

Reply
Thread Tools

In-place function

 
 
peter.mcclymont@gmail.com
Guest
Posts: n/a
 
      04-03-2006
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.

 
Reply With Quote
 
 
 
 
Pete C
Guest
Posts: n/a
 
      04-03-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      04-03-2006
(E-Mail Removed) 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()) ;
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-03-2006
(E-Mail Removed) 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.

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      04-03-2006
In article <(E-Mail Removed). com>,
(E-Mail Removed) 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.
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      04-03-2006
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
 
Reply With Quote
 
int2str@gmail.com
Guest
Posts: n/a
 
      04-03-2006

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
Probably gets bonus points from the teacher (or an instant F ):

#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

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      04-03-2006
(E-Mail Removed) 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
> Probably gets bonus points from the teacher (or an instant F ):
>
> #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!
 
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
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments