A little challenge: "Reverse a string"

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

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

#16 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: A little challenge: "Reverse a string"

Posted 02 February 2012 - 09:13 AM

That's not C++, it's C first off. Second, you should avoid using gets() because it could potentially overflow the array if you enter too many characters.
Was This Post Helpful? 1
  • +
  • -

#17 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: A little challenge: "Reverse a string"

Posted 02 February 2012 - 09:28 AM

View Postbloodzdevil, on 31 January 2012 - 03:35 AM, said:

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

As stated by vividexstance gets is bad, you should replace it with fgets(), and this is a C not C++ solution. Also you are using c-strings which is prohibited by the following.

Quote

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


Jim
Was This Post Helpful? 1
  • +
  • -

#18 OLH064  Icon User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: A little challenge: "Reverse a string"

Posted 03 February 2012 - 03:29 PM

Be nice to line two, I'm naturally user-friendly.
Spoiler

This post has been edited by OLH064: 03 February 2012 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#19 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: A little challenge: "Reverse a string"

Posted 08 February 2012 - 09:56 AM

Haskell

Spoiler


edit:
...and i didn't use spoiler tags, because i'm an idiot. fixed.

This post has been edited by Aphex19: 30 August 2012 - 02:21 PM

Was This Post Helpful? 1
  • +
  • -

#20 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 392
  • Joined: 08-January 12

Re: A little challenge: "Reverse a string"

Posted 08 February 2012 - 11:04 AM

Spoiler

Was This Post Helpful? 1
  • +
  • -

#21 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1617
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: A little challenge: "Reverse a string"

Posted 08 February 2012 - 01:15 PM

@Aphex19:

rev (x:xs) = (rev xs) ++ [x]
rev [] = []



although that is a REALLY slow algorithm, O(n^2). you can do it in O(n)

edit(I didn't make these):

fastest...
reverse l =  rev l []
  where
    rev []     a = a
    rev (x:xs) a = rev xs (x:a)



cleaner but doesn't use tail call so it's slower and has limitations. still faster than my original...
reverse                 =  foldl (flip (:)/>) []


This post has been edited by ishkabible: 08 February 2012 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#22 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: A little challenge: "Reverse a string"

Posted 08 February 2012 - 05:57 PM

Uh, you do know most of those runtime functions for a language use pointers and buffers behind the scenes?

My take:
Spoiler

Was This Post Helpful? 1
  • +
  • -

#23 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1617
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: A little challenge: "Reverse a string"

Posted 08 February 2012 - 07:12 PM

you do know that your using esi as a pointer right? :P

quite frankly, this question was ill founded as you simply can't do this without pointers. your program is using pointers behind the scenes too, even if you don't count the stack. by nature, strings REQUIRE pointers; pretty much end of story.

the only way I can think of would be to write a program in the boot sector(512 bytes only) that stored values in registers where each register corresponded to an index in the string you could then swap the necessary registers.

on 32-bit x86 computers with MMX, SSE, and AVX you could get upto 352 bytes but there is no way you could fit the sequence of operations required to assign a 352 byte string and reverse it in 512 bytes. so with EXTREMLY limited functionality it *is* kinda possible but not really.

This post has been edited by ishkabible: 08 February 2012 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#24 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: A little challenge: "Reverse a string"

Posted 08 February 2012 - 07:15 PM

Yeah I know. But we all know that it can't be done without pointers. Well, maybe a dos app using interupts? Possible.
Was This Post Helpful? 0
  • +
  • -

#25 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1617
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: A little challenge: "Reverse a string"

Posted 08 February 2012 - 08:09 PM

well maybe... the interrupt descriptor table is a pointer. but it's not like you would actually be interfacing with the table itself so I don't know.

still, how would you store the string without pointers? that's the main crux of this idea I feel like.

This post has been edited by ishkabible: 08 February 2012 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#26 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: A little challenge: "Reverse a string"

Posted 08 February 2012 - 08:44 PM

I got it!!!! Write the reversed string on a wall/black/whiteboard in the reversed order... No pointers, just a wall and marker :1eye:

Ok, int21 way:
Spoiler

Was This Post Helpful? 1
  • +
  • -

#27 OLH064  Icon User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: A little challenge: "Reverse a string"

Posted 08 February 2012 - 09:39 PM

I get it, this is impossible if you take it too literally.
There's a pointer in every processor's registers that points to the next instruction, and that pointer gets incremented every clock.

Computers can't work without at least one pointer. (The reason why I like Java less than C99)
Was This Post Helpful? 0
  • +
  • -

#28 curryjl  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 02-March 09

Re: A little challenge: "Reverse a string"

Posted 15 February 2012 - 12:19 PM

I am some what new to programming and have never used a stack container. So, I stole muballitmitte's idea and tried it out. I changed the way it gets printed, also.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#29 okdaley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-May 09

Re: A little challenge: "Reverse a string"

Posted 16 February 2012 - 07:24 AM

#include <iostream>
using namespace std;

void strReverse( char *s)
{
	string buffer;
	char *endStr;	
	int length=strlen(s);
	endStr=s+length-1;

	for ( ; length > 0; --length)
	{
		buffer.append((const char* )endStr, 1);
		endStr--;
	}
	strcpy(s, buffer.c_str());
}
int main()
{
	char MyString[] = "This is my string";
	cout << MyString << endl;
	strReverse(MyString);
	cout << MyString << endl;
}




Best Regards,

Orion K. Daley
Was This Post Helpful? 0
  • +
  • -

#30 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: A little challenge: "Reverse a string"

Posted 18 February 2012 - 08:58 PM

The original challenge said "reverse a string", not "print a string backwards", so I'd do it like this:

Spoiler

This post has been edited by r.stiltskin: 18 February 2012 - 09:00 PM
Reason for edit:: added code tags :)

Was This Post Helpful? 0
  • +
  • -

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