// New2.cpp : Defines the entry point for the console application.
//

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

void reverse(string& s);

int _tmain(int argc, _TCHAR* argv[])
{	
	string st="Word" ;
	cout<<st<<"\n";
	reverse(st);
	cout<<st<<"\n";
	return 0;

}

void reverse(string& st)
{
	int i; 
	int j = 1;
	for(i=0; i<strlen(str); i++, j++);
	rev[strlen-[j]=st[i];
	for(i=0; <strlen(st); i++);
		str[i]=rev[i];
	st="New World";
	return;
}

I have the reverse function wrong. Please hekp me with this, I am very new to programming.
**If you could please how me how to ask for a string in an array and reverse it or add two strings.
Thank you very much.:cool:

Recommended Answers

All 7 Replies

I think that you can use a stack.
First you can put the characters of the string in a stack and then to remove them from the stack. Stack is Last In First Out so after you do that process your string will be reversed.

I am sorry but I dont know what you are saying.

for(x=length-1; x>1; x--){ //print the array backward

cout<<strArray[x]<<" ";

I think this works for an array but i dont have an array. Plz help. Could be with an array.

For the sake of getting you doing the right thing, I'll suggest a mnemonic. Conditional and loop statements can be treated syntactically the same as functions. The "tag" isn't terminated with a semicolon, and the body is surrounded with braces:

void reverse(string& st)
{
  int i; 
  int j = 1;
  for(i=0; i<strlen(str); i++, j++)
  {
    rev[strlen-[j]=st[i];
  }
  for(i=0; <strlen(st); i++)
  {
    str[i]=rev[i];
  }
  st="New World";
  return;
}

This mnemonic is useful for following the best practice while avoiding the particular error that you have, which is accidentally ending the loop tag with a semicolon:

for ( i = 0; i < n; i++ ); // Wrong!

Note that loops don't have to have braces, and the first statement after the loop tag will be the body of the loop. The above wrong code is treated like so:

for ( i = 0; i < n; i++ )
{
  ;
}

Hopefully you can locate the error in the code you posted now.

commented: using strlen within a for loop, naughty naughty! -4
commented: iamthwee should read the OPs code +9

Am i suppose to fix the one you gave me?

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

void reverse(char* s)

{
  if(*s != '\0')
    reverse(s+1);
    cout<<*(s);
}

int main()

{
   reverse("ass");

   return 0;

}

The above works but I dont know how to make it ask for the string. Thanks very much for helping.
What I need my program to do is ask the user to enter a string in MAIN and sends it to a function that reverse it and send back to the main printed.
Thanks again.

std::string& Reverse(std::string& s)
{
    std::string::iterator 
        f = s.begin(),
        r = s.end();
    while (f < r)
        std::swap(*f++,*--r);
    return s;
}
std::string Reversed(const std::string& s) {
    std::string temp(s);
    return Reverse(temp);
}

Try this :

#include <algorithm>
#include <string>
#include <iostream>

using namespace std;

int main()
{
	string str = "abcdefg";
	cout << str << endl;

	reverse(str.begin(), str.end());
	cout << str << endl;

	return 0;
}

I think it's the simplest way to do your job.

Member Avatar for iamthwee

Try this:

#include <iostream>
#include <string>
#include <stack>

class Foo
{
public:
   void bar ( std::string t )
   {
      std::stack <char> arr;
      int p = t.length();

      for ( int i = 0; i < p; i++ )
      {
         arr.push ( t[i] );
      }

      for ( int i  = 0; i < p; i++ )
      {
         std::cout << arr.top();
         arr.pop();
      }
   }
};

int main()
{
   Foo test;
   test.bar ( "Another convoluted example that the OP is unlikely to be able to use" );

   std::cin.get();
   return 0;
}
commented: works for me +29
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.