1

I have write this code. It does not show any error but string is not reversed.please let me know where i have made the mistake? I'm using codeblock to write code and GCC compiler. I have created two functions reverseString and printString, the printString function is working but reverseString is not working. what can i do?

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

void reverseString(string s, int iNode, int lNode){
    while(iNode < lNode){
        char temp = s[iNode];
        s[iNode] = s[lNode];
        s[lNode] = temp;
        iNode++;
        lNode--;
    }
}

void printString(string s, int n){

    for(int i=0; i<n; i++){
        cout << s[i];
        cout << endl;
    }
}

int main() {
  string s;
  cout << "Type String To Reverse: \n";
  cin >> s;
  cout << "String In Actual Order: \n";
  printString(s,s.length());
  reverseString(s, 0, s.length()-1);
  cout << "String In Reverse Order: \n";
  printString(s,s.length());

  return 0;
}
1
  • 4
    You passed string by value, meaning you created a full copy of a string and made some operations on it, meanwhile your initial string remained unchanged. Your reverseString func should have this signature: void reverseString(string& s, int iNode, int lNode) . Also see here Commented May 6, 2022 at 19:12

1 Answer 1

3

This function accepts an object of the type std::string by value

void reverseString(string s, int iNode, int lNode){
    while(iNode < lNode){
        char temp = s[iNode];
        s[iNode] = s[lNode];
        s[lNode] = temp;
        iNode++;
        lNode--;
    }
}

It means that it deals with a copy of the string used as an argument and changes the copy instead of the original string.

You have to declare the parameter as having reference type

void reverseString(string &s, int iNode, int lNode){
    while(iNode < lNode){
        char temp = s[iNode];
        s[iNode] = s[lNode];
        s[lNode] = temp;
        iNode++;
        lNode--;
    }
}

The function could be defined simpler (without using standard features as for example std::swap) the following way

std::string & reverseString( std::string &s )
{
    for ( std::string::size_type i = 0, n = s.size(); i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n - i - 1];
        s[n - i - 1] = c;
    }

    return s;
} 

And the function can be called like

cout << "String In Reverse Order: \n";
std::cout << reverseString( s ) << '\n';

Without using the loop you could define the function just the following way

std::string & reverseString( std::string &s )
{
    return s.assign( s.rbegin(), s.rend() );
}

As for your own function printString then it could be defined at least like

std::ostream & printString( const std::string &s, std::ostream &os = std::cout )
{
    for ( char c : s )
    {
        os << c;
    }

    return os;
}

and can be called like

printString( s ) << '\n';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.