Simple Backward String

Display an inputted string backwards

Page 1 of 1

3 Replies - 2698 Views - Last Post: 27 May 2007 - 06:12 AM Rate Topic: -----

#1 lls  Icon User is offline

  • New D.I.C Head

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

Simple Backward String

Post icon  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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • 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[]; :D try using a class instead of that global variable, or pass by reference.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1