# A little challenge: "Reverse a string"

• (7 Pages)
• 1
• 2
• 3
• Last »

## 93 Replies - 56615 Views - Last Post: 08 November 2015 - 09:13 AM

### #1 JaKWaC

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

# A little challenge: "Reverse a string"

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

Reputation: 38
• 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

### #3 JaKWaC

Reputation: 76
• 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

### #4 muballitmitte

• D.I.C Regular

Reputation: 174
• 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

### #5 JaKWaC

Reputation: 76
• 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.

### #6 Bench

• D.I.C Lover

Reputation: 944
• Posts: 2,464
• 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

### #7 baavgai

• Dreaming Coder

Reputation: 7112
• Posts: 14,827
• 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

### #8 JaKWaC

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

## Re: A little challenge: "Reverse a string"

Posted 21 April 2011 - 12:38 PM

baavgai, 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

### #9 TMKCodes

• D.I.C Regular

Reputation: 48
• 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

### #10 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• 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"

### #11 desirocks

Reputation: 0
• 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...

### #12 JohnS_

Reputation: 0
• 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

```

### #13 sk1v3r

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

## Re: A little challenge: "Reverse a string"

Posted 25 April 2011 - 06:45 AM

JohnS_, on 22 April 2011 - 04:21 AM, said:

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

```

Spoiler

how about slightly less lua XD

### #14 Krishs

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

## Re: A little challenge: "Reverse a string"

Posted 04 January 2012 - 06:15 AM

well , may be

Spoiler

### #15 bloodzdevil

Reputation: 1
• 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