A little challenge: "Reverse a string"

  • (6 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6

86 Replies - 41505 Views - Last Post: 27 August 2013 - 02:43 PM

#46 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A little challenge: "Reverse a string"

Posted 05 March 2012 - 04:13 PM

Thanks for finding that. At least we can learn something from this otherwise pointless argument.
Was This Post Helpful? 1
  • +
  • -

#47 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: A little challenge: "Reverse a string"

Posted 05 March 2012 - 08:42 PM

To follow up...

I was trying to think of a reason for that clause(other than not breaking legacy code) and I believe it's an optimization that allows using things like memcpy, memset, and memmove with iterators of containers that have this clause(one of the reasons std::vector is so fast). The idea is that there would be class template called something like 'contiguous_iter', containers like std::string and std::vector would use it, and the STL algorithms can use specializations, overloads or SFINE(not sure which would be used, maybe a combo) to select the fastest algorithm. I know VC++'s STL does this for std::vector; I bet it does the same thing with std::string sense they could easily have the same iterator implementation.

This post has been edited by ishkabible: 05 March 2012 - 08:44 PM

Was This Post Helpful? 0
  • +
  • -

#48 Ricky65  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 115
  • Joined: 03-June 10

Re: A little challenge: "Reverse a string"

Posted 06 March 2012 - 04:06 PM

It's worth noting that if std::string wasn't required to be contiguous then the "data" and "c_str" member functions wouldn't be possible.
Was This Post Helpful? 0
  • +
  • -

#49 vividexstance  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 663
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: A little challenge: "Reverse a string"

Posted 07 March 2012 - 10:48 AM

View PostRicky65, on 06 March 2012 - 06:06 PM, said:

It's worth noting that if std::string wasn't required to be contiguous then the "data" and "c_str" member functions wouldn't be possible.

In my opinion this statement is false. Bjarne Stroustrup says in his book "The C++ Programming Language: Special Edition" on page 589 that:

Quote

The data() function writes the characters of the string into an array and returns a pointer to that array. The array is owned by the string, and the user should not try to delete it. The user also cannot rely on its value after a subsequent call on a non-const function on the string. The c_str() function is like data(), except that it adds a 0(zero) at the end as a C-string-style terminator.

Maybe I'm interpreting it wrong, but hypothetically speaking if the string was implemented as lets say a linked list, it would still be possible to construct an array from that linked list and return a pointer to the first element of that array.

This post has been edited by vividexstance: 07 March 2012 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#50 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: A little challenge: "Reverse a string"

Posted 07 March 2012 - 06:32 PM

I interpret that the same way but I can't figure out how you would do it. if you create a reference for deleting the newly allocate pointer later then c_str can't be constant.
Was This Post Helpful? 0
  • +
  • -

#51 vividexstance  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 663
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: A little challenge: "Reverse a string"

Posted 08 March 2012 - 01:22 PM

There are ways of making things const and not const, and even const member functions that actually might change something. Also, I think one could use statically allocated storage for the array just using the size of the linked-list or whatever data structure being used.
Was This Post Helpful? 0
  • +
  • -

#52 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A little challenge: "Reverse a string"

Posted 08 March 2012 - 02:05 PM

Isn't this horse dead yet? :rolleyes2:

OK, since I guess I was the perpetrator of this silly idea ...

I don't think we would want to have a linked list and a "permanent" array since linked list was intended to avoid an array in the first place. To satisfy c_str() we need a const array, but it only needs to be const until the next non-const method is called. So how about a dynamically allocated array that's initialized when c_str() is called (only if it isn't already non-null, as would be the case if c_str() is called multiple times with no intervening non-const method calls), and deleted by calling a private "destroyer" method whenever any non-const method is called.

This post has been edited by r.stiltskin: 08 March 2012 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

#53 Ricky65  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 115
  • Joined: 03-June 10

Re: A little challenge: "Reverse a string"

Posted 11 March 2012 - 01:59 PM

View Postvividexstance, on 07 March 2012 - 10:48 AM, said:

View PostRicky65, on 06 March 2012 - 06:06 PM, said:

It's worth noting that if std::string wasn't required to be contiguous then the "data" and "c_str" member functions wouldn't be possible.

In my opinion this statement is false. Bjarne Stroustrup says in his book "The C++ Programming Language: Special Edition" on page 589 that:

Quote

The data() function writes the characters of the string into an array and returns a pointer to that array. The array is owned by the string, and the user should not try to delete it. The user also cannot rely on its value after a subsequent call on a non-const function on the string. The c_str() function is like data(), except that it adds a 0(zero) at the end as a C-string-style terminator.

Maybe I'm interpreting it wrong, but hypothetically speaking if the string was implemented as lets say a linked list, it would still be possible to construct an array from that linked list and return a pointer to the first element of that array.

Sure, we could have a member function of a string class implemented with a linked list that returns a pointer to the first element in the relevant array.

However, the data() and c_str() member functions are usually passed to C functions which expect a contiguous built-in C array. This is the reason why only the contiguous collections, std::vector, std::array and std::string have the data() member function.

I would also like to add that since C++11, data() and c_str() perform the same function. data(), like c_str(), now appends a terminating null character.
Was This Post Helpful? 0
  • +
  • -

#54 Kiakime  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 27-February 12

Re: A little challenge: "Reverse a string"

Posted 18 March 2012 - 06:53 AM

Are vectors forbidden? 8) If not, this works.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#55 vividexstance  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 663
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: A little challenge: "Reverse a string"

Posted 18 March 2012 - 08:22 AM

You could do without that do-while loop at the end:
Spoiler

This post has been edited by vividexstance: 18 March 2012 - 08:24 AM

Was This Post Helpful? 0
  • +
  • -

#56 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: A little challenge: "Reverse a string"

Posted 29 March 2012 - 08:31 PM

You can pack your characters into a unsigned integer and pop them out as if they were a stack, this will print them in reverse.

Of course this only works for a 4-char string, but it was still fun :)

#include <iostream>

/* 4 character stack */
class char_pack {
	unsigned int data;
public:
	char_pack() 
		: data(0) {
	}

	void push(char c) {
		data = (data << 8) | c;
	}

	char pop() {
		char c = data & 0xFF;
		data = data >> 8;
		return c;
	}

	void print() {
		char c;
		if((c = pop()) != 0) {
			std::cout<<c;
			print();
		}
	}
};

int main() {

	char_pack str;
	str.push('t'); str.push('e'); str.push('s'); str.push('t');
	str.print(); //prints stack, which is the reversed string

	std::cin.ignore();
	std::cin.get();

	return 0;
}


This post has been edited by jjl: 01 April 2012 - 02:26 PM

Was This Post Helpful? 3
  • +
  • -

#57 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: A little challenge: "Reverse a string"

Posted 30 March 2012 - 06:41 AM

best solution yet, that truly does not require pointers!
Was This Post Helpful? 0
  • +
  • -

#58 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A little challenge: "Reverse a string"

Posted 31 March 2012 - 10:25 PM

View Postjjl, on 29 March 2012 - 10:31 PM, said:

You can pack your characters into a unsigned integer and pop them out as if they were a stack, this will print them in reverse.

Of course this only works for a 4-char string, but it was still fun :)


Excellent!
Was This Post Helpful? 0
  • +
  • -

#59 Donanza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-April 12

Re: A little challenge: "Reverse a string"

Posted 22 April 2012 - 08:06 AM

Here is my solution:

#include <iostream>

using namespace std;

int main()
{
    int j = 0;
    int i;
    cout << "Enter a word or a sentence: ";
    string word;
    getline(cin, word);
    for (i = word.size() - 1; i >= 0; i--)
    {
        cout << word[i];
    }
    cout << "\n\n";
    main();
}



But i need help with the palindrome. Anyone knows how to do it?
Was This Post Helpful? 0
  • +
  • -

#60 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: A little challenge: "Reverse a string"

Posted 23 April 2012 - 09:10 PM

Quote

Here is my solution:

Your using a string on line #10
Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6