-2

Please tell me why my code to reverse the input string is giving me various errors.

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
void ReverseString(string &aString);
int main(){
    string info;
    cout << "What's your string?" << endl;
    getline(cin, info);
    ReverseString(info);
    cout << ReverseString(string info) << " compare with: " << info << endl;
    system("pause");
    return 0;
}

void ReverseString(string &aString){

for(int i = 0; i < aString.length(); i++)
    {
        string temp = 0; // initialize temporary string
        temp = temp + aString.at(aString.length() - 1 - i); // hold temporary string
        if(i => aString.length()) /*assign temp string to aString when all chars are processed*/
        {
            temp = &aString;
        }

    }

}
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
NewProgrammer
  • 39
  • 1
  • 6

4 Answers4

3

Hi you could simplify your code a lot by using the STL

for example:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    std::string str = "Hello World";
    cout << str << endl; 
    std::reverse(str.begin() , str.end());
    cout << str << endl;
   return 0;
}

let me know if this is not suitable to your needs as theres a few other ways to do it too.

Without STL:

There are some corrections/changes to your code required, which I have supplied below. However you may want to look at some documentation on referencing variables to get an idea of how it works, such as:

http://www.cprogramming.com/tutorial/references.html

http://www.thegeekstuff.com/2013/05/cpp-reference-variable/

http://en.wikipedia.org/wiki/Reference_(C++)

What is a reference variable in C++?

http://www.tutorialspoint.com/cplusplus/cpp_references.htm

Correct reference and pointer use is a major part of C++ and allows for some of the most powerful functionality in the language, provided it is used correctly, or major headaches and mental scarring if used incorrectly, so it is worth, even essential, to have a firm grasp of them.

And even then expect the odd misuse to crop up every-so-often. :)

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
void ReverseString(string &aString);
int main(){
    string info;
    cout << "What's your string?" << endl;
    getline(cin, info);
    cout << info << " compare with: ";
    ReverseString(info);
    cout << info << endl;
    system("pause");
    return 0;
}

void ReverseString(string &aString)
{

    int len = aString.length();
    string temp = aString;// initialize temporary string
    aString ="";
    for(int i = 0; i < len; i++)
    {
        aString += temp[len - (1+ i)]; // assigns the reversed value to the referenced string
    }
}

Just noticed the quote below from @zac-howland : so true, I have however left the code in as an illustrative piece. Provided some reading is done on this as well as plenty of experimentation I hope NewProgrammer will get the information and skill-set he needs to go forward.

Community
  • 1
  • 1
GMasucci
  • 2,834
  • 22
  • 42
1
#include<iostream>
#include<string>
#include <algorithm>
using namespace std;

string info;
string rvrs(string &str)
{
    std::reverse(str.begin(),str.end());
    return str;
}
int main()
{
    cout<<"What is your string :: ";
    getline(cin,info);
    cout<<rvrs(info);
    cout<<endl; 
return 0;
}
Manish Bhadani
  • 437
  • 6
  • 13
0

You have a number of problems.

    string temp = 0; // initialize temporary string

It doesn't really make sense to initialize a string to 0. Just string temp; would be fine here.

    temp = temp + aString.at(aString.length() - 1 - i); // hold temporary string

That's not quite how I'd do things, but I guess it should work.

    if(i => aString.length()) 

This condition doesn't seem to make sense. Your loop is defined to iterate with i going from 0 to the length of the string -1, so it can never be greater than or equal to the string length.

    /*assign temp string to aString when all chars are processed*/
    {
        temp = &aString;
    }

Here the code doesn't match the comment. The comment says you're going to assign to aString, but the code assigns something to temp. The comment is probably closer to what you really want. But you still need to fix the condition, and probably want to do this after the loop has finished executing. So in pseudo-code, you'd end up with something like:

for (all characters in the string)
    add the next character in the string to the end of temp
assign temp back to the original string
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

You have a few syntactical errors in addition to your logical ones:

cout << ReverseString(string info) << " compare with: " << info << endl;

ReverseString(string info) will pass in an empty string to your ReverseString function (if it even compiles - which looks like it should not since you have 2 info's in the same scope). What you wanted is:

cout << ReverseString(info) << " compare with: " << info << endl;

In your reverse function, you only need to go to length() / 2.

Since you are passing by reference, changes you make to the string within the function will be reflected in the object you passed into it. That is, the original info will be reversed. If you want it to operate on a copy, you need to pass it by copy, not by reference.

Finally, cout << ReverseString(info) is not useful (if it even compiles) as ReverseString returns a void. You should have it return a string (the reversed string).

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • The reason `cout << ReverseString(string info)` doesn't compile is nothing to do with having 2 `info`'s in the same scope. It's a function declaration in the middle of an expression. It's complete nonsense, the OP simply doesn't know basic C++. – Benjamin Lindley Nov 12 '13 at 15:52
  • @BenjaminLindley Understood. There were so many syntactical issues with that statement alone that I didn't want to even attempt to list them all. – Zac Howland Nov 12 '13 at 16:02