# String Reverse Without Declaring a Local Variable or calling another function?

**URL:** <https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397>\
**Category:** Factual Questions\
**Created:** [September 28, 2004, 1:20am UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397 "2004-09-28T01:20:46Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Squink](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Squink](https://boards.straightdope.com/u/Squink)\
**Post date:** [September 28, 2004, 8:34pm UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/21 "2004-09-28T20:34:17Z")

</div>

> [@t-bonham@scc.net](#):
>
> That’s a very bad solution!

I suppose that going straight to assembly language and using a couple of registers would be a good way _not_ to land the job either.

---

<div class="post-metadata">

**Author:** ![LordVor](https://avatars.discourse-cdn.com/v4/letter/l/90ced4/32.png) [@LordVor](https://boards.straightdope.com/u/LordVor)\
**Post date:** [September 28, 2004, 9:01pm UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/22 "2004-09-28T21:01:19Z")

</div>

> [@t-bonham@scc.net](#):
>
> That’s a very bad solution!  
> You should _not_ define a variable as GLOBAL when it is used only as a temp swap variable in a local piece of code.

Well, any solution to the original question is going to be a bad coding practice. Mine’s a whole lot better than, say, the recursive solution.

-lv

---

<div class="post-metadata">

**Author:** ![Cooper](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Cooper](https://boards.straightdope.com/u/Cooper)\
**Post date:** [September 28, 2004, 11:13pm UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/23 "2004-09-28T23:13:27Z")

</div>

> [@LordVor](#):
>
> Well, any solution to the original question is going to be a bad coding practice. Mine’s a whole lot better than, say, the recursive solution.
> 
> -lv

It could be they were looking for any “outside the box solution”, but probably they phrased the question more specifically than it was related to me. I think the answer they wanted was really the recursive solution - it doesn’t break any rules (true, I could run out of stack space but hypothetically I could run out of anything) and is completely self-contained. I think inlining assembler and accessing registers would escape them and they’d have to give credit where due…of course if they chose to run your program on a different processor than you’d inlined for it wouldn’t work. I’m not sure inlining is really “C” (I am not saying, more asking).

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [September 29, 2004, 2:28am UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/24 "2004-09-29T02:28:30Z")

</div>

> [@Cooper](#):
>
> I’m not sure inlining is really “C” (I am not saying, more asking).

It isn’t, at least not in that sense (inlining assembler). It’s a common extension, but the Standard is silent on the issue (I think) and it certainly isn’t a way to write portable code.

The `inline’ keyword in relation to inlining functions (that is, giving the compiler a hint that this function ought to be inlined) was introduced in C99 and was, again, a common extension before that. (The fact that good compilers ignore such hints can be glossed over here.)

---

<div class="post-metadata">

**Author:** ![Squink](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Squink](https://boards.straightdope.com/u/Squink)\
**Post date:** [September 29, 2004, 3:25am UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/25 "2004-09-29T03:25:40Z")

</div>

> [@Derleth](#):
>
> It isn’t, at least not in that sense (inlining assembler). It’s a common extension, but the Standard is silent on the issue (I think) and it certainly isn’t a way to write portable code.

My 1978 copy of Kernighan & Ritchie includes just this on asm:

> [@](#):
>
> Some implementations also reserve the words **fortran** and **asm**.

Harbison & Steele (1987) includes this:

> [@](#):
>
> The former reserved words **entry, asm,** and **fortran** are now seldom seen, although **asm** does appear occasionally.

---

<div class="post-metadata">

**Author:** ![tim314](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tim314/32/3468_2.png) [@tim314](https://boards.straightdope.com/u/tim314)\
**Post date:** [September 29, 2004, 6:00am UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/26 "2004-09-29T06:00:11Z")

</div>

For those who enjoy pointless programming problems:

Several years ago I had a programming job for the summer. I wasn’t used to spending that many hours a day staring at code, and it must have been affecting me somewhat, because I started having weird programming dreams. In one, I was trying to figure out a way to sort an array of unsigned integers into ascending order. But here’s the catch: I wasn’t allowed to use \<, \>, or ==, or to include elements of the array in the argument list of any function (not even indirectly, such as setting a variable equal to an expression that contains some of the numbers, and then passing it to a function.)

Don’t ask me why anyone would want to do such a stupid thing – like I said, it was a dream. The really funny part is that I actually figured out a way to do it while in my dream, and it actually works.

I’ll wait a little while before posting it, in case anyone wants to figure it out for themselves.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [September 29, 2004, 6:10am UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/27 "2004-09-29T06:10:51Z")

</div>

**Squink** : Since the first C standard was created in 1989, a book from 1978 (or 1987) could not be speaking of Standard C.

Plus, I hope you have more recent C references.

---

<div class="post-metadata">

**Author:** ![Squink](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Squink](https://boards.straightdope.com/u/Squink)\
**Post date:** [September 29, 2004, 6:33am UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/28 "2004-09-29T06:33:25Z")

</div>

> [@Derleth](#):
>
> a book from 1978 (or 1987) could not be speaking of Standard C.  
> Plus, I hope you have more recent C references.

K&R was sort of the standard in the days before standards. (Yeah, I KNOW :smack: )  
Oh yes, I have newer, but I can’t bring myself to toss such oldies but goodies.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [September 29, 2004, 3:30pm UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/29 "2004-09-29T15:30:42Z")

</div>

> [@tim314](#):
>
> Several years ago I had a programming job for the summer. I wasn’t used to spending that many hours a day staring at code, and it must have been affecting me somewhat, because I started having weird programming dreams. In one, I was trying to figure out a way to sort an array of unsigned integers into ascending order. But here’s the catch: I wasn’t allowed to use \<, \>, or ==, or to include elements of the array in the argument list of any function (not even indirectly, such as setting a variable equal to an expression that contains some of the numbers, and then passing it to a function.)

It’s not that hard to do. For those who want a little hint, consider this: What is the value of (a + b + |a - b|)/2?

---

<div class="post-metadata">

**Author:** ![LtningBug](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@LtningBug](https://boards.straightdope.com/u/LtningBug)\
**Post date:** [October 8, 2004, 5:18am UTC](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397/30 "2004-10-08T05:18:41Z")

</div>

> [@tim314](#):
>
> For those who enjoy pointless programming problems:
> 
> Several years ago I had a programming job for the summer. I wasn’t used to spending that many hours a day staring at code, and it must have been affecting me somewhat, because I started having weird programming dreams. In one, I was trying to figure out a way to sort an array of unsigned integers into ascending order. But here’s the catch: I wasn’t allowed to use \<, \>, or ==, or to include elements of the array in the argument list of any function (not even indirectly, such as setting a variable equal to an expression that contains some of the numbers, and then passing it to a function.)
> 
> Don’t ask me why anyone would want to do such a stupid thing – like I said, it was a dream. The really funny part is that I actually figured out a way to do it while in my dream, and it actually works.
> 
> I’ll wait a little while before posting it, in case anyone wants to figure it out for themselves.

To get (a \< b), you can do ((a - b) & 0x80000000) (assuming 32 bit integers).

I dunno if that goes against the spirit of your requirements, though, since it’s basically the same thing.

[Previous page](https://boards.straightdope.com/t/string-reverse-without-declaring-a-local-variable-or-calling-another-function/266397.md?page=1)
