A little challenge: "Reverse a string"

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

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

#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: 1622
  • View blog
  • Posts: 5,709
  • 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: 1072
  • View blog
  • Posts: 4,532
  • 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: 91
  • 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: 1622
  • View blog
  • Posts: 5,709
  • 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 offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,989
  • 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
  • +
  • -

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