13 Replies - 54430 Views - Last Post: 05 April 2005 - 02:02 PM Rate Topic: -----

#1 GeryGoose  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 24-February 05

Reverse String Function in C++

Posted 04 April 2005 - 07:27 PM

okay guys... I've been working on this code snippet but i gave up on it a while back and now decided to give it another shot. I have a not-so-good version in the code snippets already but i wanted to implement a function to have it easily work and have the user put in what ever string they wanted to use it multiple times they could... uhm also the syntax isn't working when it should put it back into a string. I'm gonna be working on it most of the night so I can update every once in a while.

#include <iostream.h>
#include <string.h>

char your_string[256];
char new_string[256];
void ReverseString();

void main()
{
	cin.getline(your_string, 256);
	ReverseString();
	cout << your_string << "\n";
}

void ReverseString()
{
	int x = strlen(your_string);
	x--;
	for(int y = x; y >= 0; y--)
	{
  your_string[y] += your_string[y];
	}
}

Attached File(s)

  • Attached File  main.cpp (363bytes)
    Number of downloads: 61

This post has been edited by syntax_error: 04 April 2005 - 07:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reverse String Function in C++

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Reverse String Function in C++

Posted 04 April 2005 - 07:36 PM

I hate to ask, but why aren't you using the reverse function from the standard C++ library? You can make your own, of course, but it may be more efficient to use the one that's there.
Was This Post Helpful? 0
  • +
  • -

#3 GeryGoose  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 24-February 05

Re: Reverse String Function in C++

Posted 04 April 2005 - 07:53 PM

O_O so there is really no point in this?
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 Function in C++

Posted 04 April 2005 - 07:58 PM

well atleast you tried and learned B)

http://msdn.microsof...2c_._mbsrev.asp
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Reverse String Function in C++

Posted 04 April 2005 - 07:59 PM

Well, it depends on what you'd like to accomplish. If you're actually looking for a way to reverse a string in a program you're working on, then I would say that using the existing function from the library might be the best idea.

Creating your own function of this type, however, is a good way to familiarize yourself with both the syntax and logic of the language.

Here is an example of the exisitng one (it's one I grabbed from the net, not mine, but illustrates the use).
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int main (void)
{
char sentence[100];
cin.getline (sentence, 100);
reverse (sentence, sentence + strlen (sentence));
cout << sentence << endl;
return 0;
}


there is also the _strrev function available in the string.h library.

[edit]Whoops, Nexus was quicker than me with the _strrev![\edit]

This post has been edited by Amadeus: 04 April 2005 - 08:01 PM

Was This Post Helpful? 1

#6 GeryGoose  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 24-February 05

Re: Reverse String Function in C++

Posted 04 April 2005 - 08:01 PM

I think I will still try to work on mine just to better understand the language. So if you figured out why mine is not working, do tell.
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Reverse String Function in C++

Posted 04 April 2005 - 08:18 PM

Try this:
#include <iostream>
#include <string.h>
using namespace std;

char your_string[256];
char new_string[256];
void ReverseString();

int main()
{
cin.getline(your_string, 256);
ReverseString();
cout << new_string << "\n";
return 0;
}

void ReverseString()
{
int x = strlen(your_string)-1;
for(int y = x; y >= 0; y--)
{
 new_string[x-y] = your_string[y];
}
}


Just a small mod to your loop, you were assigning things to the same position.

As a quick fyi, the old c style includes have been deprecated...where possible in c++, it is more common to use the #include<iostream> in conjunction with using namespace std;

You may also want to try and avoid the use of global variables where possible.
Was This Post Helpful? 0
  • +
  • -

#8 GeryGoose  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 24-February 05

Re: Reverse String Function in C++

Posted 04 April 2005 - 08:50 PM

god damn amadeus... your a beast...
Was This Post Helpful? 0
  • +
  • -

#9 GeryGoose  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 24-February 05

Re: Reverse String Function in C++

Posted 04 April 2005 - 10:23 PM

So i enhanced my code to return the string in the same declared name...
if that makes any since at all... but now it is totally fucking up... I'll work on it until someone posts.

#include <iostream>
#include <string.h>
using namespace std;

char your_string[256];
int ReverseString(char *str);

int main()
{
	cin.getline(your_string, 256);
	ReverseString(your_string);
	cout << your_string << "\n";
	return 0;
}

int ReverseString(char *str)
{
	int x = strlen(str);
	x--;
	for(int y = x; y >= 0; y--)
	{
  str[x-y] = str[y];
	}
	return 0;
}


This post has been edited by syntax_error: 04 April 2005 - 10:25 PM

Was This Post Helpful? 0
  • +
  • -

#10 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 Function in C++

Posted 04 April 2005 - 10:37 PM

return the string in the same declared name...

wait, what?
Was This Post Helpful? 0
  • +
  • -

#11 GeryGoose  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 24-February 05

Re: Reverse String Function in C++

Posted 04 April 2005 - 11:03 PM

like say the string name in parameters is your_string. i wanna return the the string backwards in the same name.
Was This Post Helpful? 0
  • +
  • -

#12 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4496
  • View blog
  • Posts: 28,404
  • Joined: 21-March 01

Re: Reverse String Function in C++

Posted 04 April 2005 - 11:52 PM

clear it out and assign your manipulation variable to the variable you want. it's really kind of pointless though.
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Reverse String Function in C++

Posted 05 April 2005 - 07:02 AM

I'm gonna assume that beast comment was a compliment of a sort :)

The reason your new function is not functioning properly is because your overwriting the string in the middle. There are far more elegant solutions than what is below, but I'm afraid I'm not near a compiler right now, so had to go with what I know will work. It's essentialyy the implementation of sloth's suggestion, using a temp variable and then reassigning.
#include <iostream>
#include <string.h>
using namespace std;

char your_string[256];
char new_string[256];
void ReverseString(char *your_string);

int main()
{
cin.getline(your_string, 256);
ReverseString(your_string);
cout << "Reverse is "<<your_string << "\n";
return 0;
}

void ReverseString(char *your_string)
{
char temp[256];
int x = strlen(your_string)-1;
for(int y = x; y >= 0; y--)
{
temp[x-y] = your_string[y];
}
for(unsigned int i=0;i<strlen(your_string);i++)
   your_string[i] = temp[i];
}


You could also use a temp variable in the original loop itself to preserve integrity, or even the triple XOR method shown here.
Was This Post Helpful? 0
  • +
  • -

#14 GeryGoose  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 24-February 05

Re: Reverse String Function in C++

Posted 05 April 2005 - 02:02 PM

usuing pointers and a little help from Dark_Nexus ;) I have come up with this using no temp variables and such. I think its a very nice bit of code.

#include <iostream>
#include <string.h>
using namespace std;

int ReverseString(char *str);

int main()
{
	char your_string[256];
	cin.getline(your_string, 256);
	ReverseString(your_string);
	cout << your_string << "\n";
	return 0;
}

int ReverseString(char *str)
{
	int x = strlen(str);
	for(int y = x; y >= (x/2)+1; y--)
	{
  swap(str[x-y],str[y-1]);
	}
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1