A little challenge: "Reverse a string"

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

89 Replies - 44628 Views - Last Post: 21 January 2015 - 11:15 AM

#76 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: A little challenge: "Reverse a string"

Posted 28 November 2012 - 01:01 AM

but how do i reverse a string if it isnt a string? which means how do i avoid using pointers and arrays m confused
Was This Post Helpful? 0
  • +
  • -

#77 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1624
  • View blog
  • Posts: 5,716
  • Joined: 03-August 09

Re: A little challenge: "Reverse a string"

Posted 28 November 2012 - 08:54 AM

most of the solutions that stayed within the confines of the challenge found an alternate representation for a string.
Was This Post Helpful? 0
  • +
  • -

#78 snypeNET  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 06-October 12

Re: A little challenge: "Reverse a string"

Posted 12 January 2013 - 10:17 AM

I believe this works. I didn't know if string member functions are allowed. But here is my solution. It works!

Spoiler

Was This Post Helpful? 0
  • +
  • -

#79 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1118
  • View blog
  • Posts: 4,641
  • Joined: 09-June 09

Re: A little challenge: "Reverse a string"

Posted 12 January 2013 - 05:16 PM

Quote

I believe this works. I didn't know if string member functions are allowed. But here is my solution. It works!


The problem states that you are required NOT to use arrays, pointers, or C strings. Well you are using C++ strings so I think your not very creative solution passes.
Was This Post Helpful? 0
  • +
  • -

#80 snypeNET  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 06-October 12

Re: A little challenge: "Reverse a string"

Posted 12 January 2013 - 05:34 PM

View Postjjl, on 12 January 2013 - 05:16 PM, said:

Quote

I believe this works. I didn't know if string member functions are allowed. But here is my solution. It works!


The problem states that you are required NOT to use arrays, pointers, or C strings. Well you are using C++ strings so I think your not very creative solution passes.


I don't think it's a question of creativity but does it solve the problem. And my solution solves the problem.
Was This Post Helpful? 0
  • +
  • -

#81 Limecore  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 92
  • Joined: 28-November 12

Re: A little challenge: "Reverse a string"

Posted 16 February 2013 - 12:08 AM

I haven't got the code yet, but not using the reverse function, I would use recursion, grabbing one letter, then calling itself, then printing the letter. Am not 100% sure how I would grab the letter yet, but im going to try
Was This Post Helpful? 0
  • +
  • -

#82 ashutosh23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-February 13

Re: A little challenge: "Reverse a string"

Posted 27 February 2013 - 11:18 PM

Spoiler

This post has been edited by Skydiver: 28 February 2013 - 06:19 AM
Reason for edit:: Fixed tags.

Was This Post Helpful? 0
  • +
  • -

#83 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: A little challenge: "Reverse a string"

Posted 30 June 2013 - 03:44 PM

I could have just printed it out, but I did better and used a stringstream class instead to actually persist the reversed string. I know that stringstream probably uses an internal buffer, but I couldn't think of how to do it without printing or using built in functions.
Spoiler

This post has been edited by Skydiver: 20 July 2013 - 03:46 PM
Reason for edit:: Fixed code tags. Put code in spoiler tag to hide from others tackling the challenge.

Was This Post Helpful? 0
  • +
  • -

#84 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1624
  • View blog
  • Posts: 5,716
  • Joined: 03-August 09

Re: A little challenge: "Reverse a string"

Posted 23 July 2013 - 01:27 PM

Sense I think jjl's char_pack is the only solution that really stays true to the question IMO so far I figured I would generalize his some. I actually rather seriously doubt a better solution than this exists sense anything more would require dynamic memory allocation.

#include <iostream>
#include <stdexcept>

template<int x>
struct funny_string {
  char head;
  funny_string<x - 1> rest;
  funny_string() : head('\0'), rest() {}
  funny_string& push(char c) {
    if(head == '\0') {
      head = c;
    } else {
      rest.push(head);
      head = c;
    }
    return *this;
  }
  char peek() const {
    return head;
  }
  char pop() {
    if(head == '\0') return '\0';
    char c = head;
    head = rest.pop();
    return c;
  }
};

template<>
struct funny_string<0> {
  void push(char) {
    throw std::length_error("funny_string<0>::push");
  }
  char pop() {
    return '\0';
  }
  char peek() {
    throw std::length_error("funny_string<0>::push");
  }
};

int main() {
  funny_string<30> str;
  str.push('t').push('e').push('s').push('t').push('s');
  while(str.peek()) std::cout << str.pop();
  std::cout<<'\n';
}


Any ideas on how to have a dynamic length string without pointers? sounds rather impossible if you ask me; at least if you stay within defined behavior.

edit:
I'd also like to point out that this topic has gone on for so long that when this topic was first proposed I didn't have a firm enough grasp on specialization to do this; I barely knew of its existence. This topic is OLD

This post has been edited by ishkabible: 23 July 2013 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#85 temp_0001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 25-August 13

Re: A little challenge: "Reverse a string"

Posted 26 August 2013 - 11:31 AM

simple
Spoiler

Was This Post Helpful? 0
  • +
  • -

#86 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3870
  • View blog
  • Posts: 12,234
  • Joined: 05-May 12

Re: A little challenge: "Reverse a string"

Posted 26 August 2013 - 08:49 PM

Unfortunately you used pointers. To quote from post #1 of the challenge:

Quote

How would you reverse a string without using, pointers, arrays, or c-strings.

Was This Post Helpful? 0
  • +
  • -

#87 dfleury2  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 1
  • Joined: 27-August 13

Re: A little challenge: "Reverse a string"

Posted 27 August 2013 - 02:43 PM

Destructors are called in reverse order

Spoiler

Was This Post Helpful? 3
  • +
  • -

#88 lulu1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-January 15

Re: A little challenge: "Reverse a string"

Posted 19 January 2015 - 05:27 PM

I know it's a bit too late to reply but how about this?
string word = "myplace";
	for (int i = word.length(); i > -1; i--){
		cout << word[i];
	}

Was This Post Helpful? 0
  • +
  • -

#89 Limecore  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 92
  • Joined: 28-November 12

Re: A little challenge: "Reverse a string"

Posted 19 January 2015 - 06:59 PM

View Postlulu1122, on 20 January 2015 - 11:27 AM, said:

I know it's a bit too late to reply but how about this?
string word = "myplace";
	for (int i = word.length(); i > -1; i--){
		cout << word[i];
	}


The last reply to this thread is almost 2 years old. It is good etiquette (if not a rule) to leave dead posts like this one alone, because everyone subscribed to it gets annoying email reminders about a topic that is no longer relevant to them

This post has been edited by Limecore: 19 January 2015 - 07:00 PM

Was This Post Helpful? 0
  • +
  • -

#90 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 719
  • View blog
  • Posts: 2,484
  • Joined: 31-December 10

Re: A little challenge: "Reverse a string"

Posted 21 January 2015 - 11:15 AM

View PostLimecore, on 19 January 2015 - 08:59 PM, said:

The last reply to this thread is almost 2 years old. It is good etiquette (if not a rule) to leave dead posts like this one alone, because everyone subscribed to it gets annoying email reminders about a topic that is no longer relevant to them

I agree that necro posting is bad, but this is a challenge thread, so IMO, it's not the same thing.

And to lulu1122, read the rules of the challenge very carefully! You didn't reverse the string, you simply iterated through it backwards and printed each character.
Was This Post Helpful? 0
  • +
  • -

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