1

Hey guys i get stuck in the unusual situation. This is my code, it works perfectly for returning the reverse of the string but it gives output with including the space so I don't want that space to be included in my programme output so anyone has suggestions about this plz share it... by the way this is my code :

#include <iostream>

using namespace std;

string reverse(string str, int size) {
  if (size == -1)
    return "";
  else
    {
      char a;
      a = str[size];
      return a + reverse(str, size - 1);
    }
}

int main() {
  int size;
  cout << "the size of the string : ";
  cin >> size;
  string str;
  cout << "enter the word : ";
  cin >> str;
  cout << reverse(str, size);
}
2
  • 1
    I think you should take a minute to learn how to format posts using Markdown or HTML... Commented Mar 5, 2015 at 21:05
  • 1
    Can you clarify which space you are referring to? Might be best to give sample input and the corresponding sample output. Also, I would suggest that you surround the output with single quotes so you can actually tell where the string starts and ends. Commented Mar 5, 2015 at 21:05

2 Answers 2

2

Since you use std::string, you don't need to specify the size of the string, but use the std::string::size() or std::string::length() member functions. Also, a = str[size]; is problematic when size equals to the size of the string, since you perform an out of bound access (remember that C++ uses zero-based indexing). You can simplify the code a lot, ending up with

#include <iostream>
#include <cstddef> // for std::size_t

using namespace std;

string reverse(string str, std::size_t pos) {
    return (pos == 0 ? "" : str[pos - 1] + reverse(str, pos - 1));
}

int main() {
    string str;
    cout << "enter the word : ";
    getline(cin, str); // allow for spaces in the string
    cout << reverse(str, str.size()) << endl;
}

Here, instead of using cin >> str, I used getline(cin, str), since cin reads up to the first whitespace, whereas getline allows to read strings that containg spaces.

Sign up to request clarification or add additional context in comments.

Comments

1

Change the implementation of the function reverse to the following.

string reverse(string str ,int size){
    if (size==-1)
        return "";
    else
    {
        char a;
        a=str[size];
        if (' ' == a )
            return reverse(str,size-1)
        else
            return a+reverse(str,size-1);
    }
}

Alternatively, do some pre-processing on th input.

2 Comments

e.g if i give input : "mohsin" output should be "nishom" and well that is fine it gives the same output but it includes the space before the output mohsin .............
@MohsinMushtaq Also, this is C++. The string class has a length value you can use without having to pass the size.

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.