10 Replies - 96003 Views - Last Post: 26 April 2011 - 05:24 PM Rate Topic: -----

#1 arnav_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-January 06

reverse string

Posted 31 January 2006 - 07:54 AM

i want to reverse a string without spaces.please help me im new in c++.i want to use to very basics.
Is This A Good Question/Topic? 0
  • +

Replies To: reverse string

#2 Voodoo Doll  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: reverse string

Posted 31 January 2006 - 08:04 AM

What have you tried? You must have given it a shot or you wouldn't be stuck, right? Otherwise you're not stuck and there's not really any way for us to help you since you're still able to design an algorithm, right? :) There are actually several ways to do this depending on how much extra memory you want to use. It could be as simple as copying the string in reverse to another array.

Or you could use the std::reverse() function from <algorithm>. :D The solution that you're probably looking for is a manual one, since this looks like a homework assignment, and it's clever because you need to make a few observations about how a reversal can be done. Here's a hint: Go from the outside in on both sides. :)
Was This Post Helpful? 0
  • +
  • -

#3 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: reverse string

Posted 31 January 2006 - 02:28 PM

I was screwing around with reversing strings not too long ago.

The algorithm i had reverses the words in the string though, for example "Hello World" becomes "olleH dlroW". It wasnt necessary well implemented algorithm but it got the job pretty fast. Post some code, and well help you figure it out.
Was This Post Helpful? 0
  • +
  • -

#4 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: reverse string

Posted 31 January 2006 - 07:06 PM

i hate to post code, but it is available on the snippets section.

http://code.dreaminc.../snippet102.htm
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: reverse string

Posted 31 January 2006 - 11:02 PM

Since You're new I don't think this is a good idea but just in case someone else might want to see it....this one is the shortest function and the most awesome function to reverse a string....using recursion.
Was This Post Helpful? 0
  • +
  • -

#6 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: reverse string

Posted 01 February 2006 - 02:50 AM

super sexy indeed, but for actually reversing the string in memory it takes a bit more :P recursion for the win though $$
Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: reverse string

Posted 03 February 2006 - 06:52 AM

Yup...but it sure is a wacky way to do it.
Was This Post Helpful? 0
  • +
  • -

#8 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: reverse string

Posted 03 February 2006 - 06:55 AM

For some reason I feel he just wants to get his string reversed.
Probably it isn't a homework assignment...[it probably is]
I guess we've given him enough code to get him started.

But just in the rare case that you don't need to write your own function use std::reverse() as suggested by Voodoo Doll or the strrev() function from string.h
Was This Post Helpful? 0
  • +
  • -

#9 dec1pher  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 116
  • Joined: 27-January 06

Re: reverse string

Posted 03 February 2006 - 07:09 AM

and here's a spaceless reverser code based on born2c0de's snippet:

#include <iostream>
using namespace std;
 
void ret_str(char* s)
{
    if(*s != '\0')
	{
  if(*s == ' ')
 	 *s++;

  ret_str(s+1);
  cout<<*(s);
	}
}
 
int main()
{
   ret_str("born2c0de with a space");
   cout << endl;
   system("pause");
   return 0;
}


This post has been edited by dec1pher: 03 February 2006 - 07:10 AM

Was This Post Helpful? 1

#10 Archit911  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 24-July 09

Re: reverse string

Posted 17 August 2010 - 11:40 PM

// i m writhing only logic ... ;)/> or :(/>

char str1[100] = "This is Text";
char str2[100];
int s= sizeof(str1); // gets the size of string
int i=0;
int s2=0;
//loop

for(i=s;i==0;i--)
{
// last value of str1 goes to first position of str2
str2[s2]=str1[i];
s2++;
}

printf("%s",str2);


Was This Post Helpful? 0
  • +
  • -

#11 romil797  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 24-April 11

Re: reverse string

Posted 26 April 2011 - 05:24 PM

OK! Try this I got from another website and editted:
#include <iostream>
using namespace std;
 
void ret_str(char* s)
{
    if(*s != '\0')
	{

  ret_str(s+1);
  cout<<*(s);
	}
}
 
int main()
{
   ret_str("born2c0de with a space");
   cout << endl;
   system("pause");
   return 0;
}




Edited by macosxnerd101: Please, :code:.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1