# Simple Backward String

Page 1 of 1

## 3 Replies - 3032 Views - Last Post: 27 May 2007 - 06:12 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=28336&amp;s=64fd579bbb74d9fb39ca27796182d68b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lls

Reputation: 0
• Posts: 1
• Joined: 23-May 07

# Simple Backward String

Posted 23 May 2007 - 10:31 PM

i know this is a super-simple problem, but my code is messed up somewhere...i just need it to output the string in the reverse order of its input.

```#include <iostream>
#include <cmath>

using namespace std;

char str;

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

int main()
{
cout << "Enter your string: ";
cin >> str;
cout << endl;
cout << "Backwards, your string is: " << reverse << endl;

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Simple Backward String

### #2 musya

• D.I.C Lover

Reputation: 11
• Posts: 1,012
• Joined: 25-April 07

## Re: Simple Backward String

Posted 24 May 2007 - 12:18 AM

first of all try not to use global variables, they can be overrriden easily and two your reverse function does not return anything, it returns 0, you need to specify a string return type and create a temporary string for your swaped characters. then return that string, or it could be an array of characters. and plus str needs to be an array. so str[]; try using a class instead of that global variable, or pass by reference.

### #3 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Simple Backward String

Posted 27 May 2007 - 02:13 AM

Let me echo "try not to use global variables". In your program you use the variable name str twice, once for a global vaiable, and once for an argument to a function -- already there is potential for desaster.

you declare str as: char str; which is just a single character not a string, so the line:
cin >> str; will only read in one character, not a nice long string to reverse. Infact cin is not the best way to get a C-String since it does not care about length. IF you want to use cin that you should use the String class, else use cin.getline(char *Buff, streamsize n) which allows you to set a limit to the size of the input:
```#include <iostream>
using namespace std;
int main()
{
char InputBuffer[256]; //a buffer to allow strings up to 255 characters long.
cout << "Please Enter a String: ";
cin.getline(InputBuffer, 255);
return 0;
}
```

Since your reverse function deals with pointer there is no need to return anything and you can declare its return value as void. The logic is rather simple start at the end and work your way back to half way swaping with the first half... You logic is ALMOST this.

(x/2)++ is a syntax error and has no meaning... you can increment an expression. you may mean (x/2)+1. Note that (x/2)++ would mean (x/2) = (x/2) + 1 and you can not assign a value to (x/2).

both your for loop and the swap statment have y-- which basicly makes y decrement by 2 rather than 1. Remove it from within the swap.

### #4 Pontus

• Cattlebruiser

Reputation: 18
• Posts: 612
• Joined: 28-December 06

## Re: Simple Backward String

Posted 27 May 2007 - 06:12 AM

```string phrase = "hello";
string reverse;
for(int a =0;a<phrase.size();a++){
revrese+=phrase[phrase.size()-a];
}
```

This post has been edited by manhaeve5: 27 May 2007 - 06:12 AM