A little challenge: "Reverse a string"

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

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

#1 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

A little challenge: "Reverse a string"

Post icon  Posted 21 April 2011 - 12:36 AM

I went out drinking with a few friends and being the nerds we are one of my friends posed the question:

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

After I got back I sat down and tried to tackle the question. Took me a little bit but after a while I came up with a solution.

I'd be interested to see what others would do to solve this question.

The terms "reverse" and "string" are used very loosely.

edit by ishkabible:
please remmber to post solutions in spoiler tags when
[ spoiler ]
your code here

[ /spoiler]

Is This A Good Question/Topic? 4
  • +

Replies To: A little challenge: "Reverse a string"

#2 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 12:57 AM

Hi,
I haven't more knowledge of c and c++ but i did it in C#
like this
Spoiler


edit by ishkabible: please post all solutions to challenges in spoiler tags

This post has been edited by ishkabible: 21 April 2011 - 01:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 12:59 AM

Your using a string at line 4 ;)
Was This Post Helpful? 0
  • +
  • -

#4 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 02:29 AM

what you posted is really confusing so it would be best if you write a clear problem statement. otherwise people will just write solutions like the following (pseudoc++)

Spoiler

This post has been edited by ishkabible: 21 April 2011 - 01:14 PM

Was This Post Helpful? 2
  • +
  • -

#5 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 09:14 AM

I'm pretty sure the idea is for the question to be fairly vague and open ended. There are a ton of ways you can solve the question and your solution works as far as I'm concerned.
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 12:12 PM

the C++ standard library has got a built-in reverse function, that's less of a challenge and more like common sense to be honest :)
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5941
  • View blog
  • Posts: 12,869
  • Joined: 16-October 07

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 12:36 PM

Spoiler

This post has been edited by ishkabible: 21 April 2011 - 01:15 PM

Was This Post Helpful? 4
  • +
  • -

#8 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 12:38 PM

View Postbaavgai, on 21 April 2011 - 12:36 PM, said:

Spoiler


Ya thats basically what I did, but also put in a little check to not print out the new line character.

Spoiler


Its the simplest way I can find to do it with no pointers at all (include nothing that relies on pointers as well)

This post has been edited by ishkabible: 21 April 2011 - 01:16 PM

Was This Post Helpful? 1
  • +
  • -

#9 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 12:57 PM

Duh, i'll just use integer type for the temp value and cast the character as integer and back. See no strings, pointers or arrays
Was This Post Helpful? 0
  • +
  • -

#10 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,714
  • Joined: 03-August 09

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 01:17 PM

just in case i haven't made my point, please post solutions in spoiler tags, just like code tags expect you use "spoiler" instead of "code"
Was This Post Helpful? 0
  • +
  • -

#11 desirocks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-June 09

Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 04:29 PM

>>> str = 'ILOVELINUX'
>>> print str[::-1]
XUNILEVOLI



This is Python and this is called slicing...
Was This Post Helpful? 0
  • +
  • -

#12 JohnS_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-April 11

Re: A little challenge: "Reverse a string"

Posted 22 April 2011 - 04:21 AM

function reverse(str)
	return string.reverse(str);
end



how about lua
Was This Post Helpful? 0
  • +
  • -

#13 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: A little challenge: "Reverse a string"

Posted 25 April 2011 - 06:45 AM

View PostJohnS_, on 22 April 2011 - 04:21 AM, said:

function reverse(str)
	return string.reverse(str);
end



how about lua

Spoiler

how about slightly less lua :P XD
Was This Post Helpful? 0
  • +
  • -

#14 Krishs  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 105
  • Joined: 06-April 10

Re: A little challenge: "Reverse a string"

Posted 04 January 2012 - 06:15 AM

well , may be

Spoiler

Was This Post Helpful? 1
  • +
  • -

#15 bloodzdevil  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 12-June 10

Re: A little challenge: "Reverse a string"

Posted 31 January 2012 - 02:35 AM

Guys I dont know much of C++, just a beginner.
I'd do it like this:
Spoiler

This post has been edited by bloodzdevil: 31 January 2012 - 02:40 AM

Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »