1

i m trying to create a reverse string code but it doesnt output anything

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

int main(){
  string name = "dog";
  string reverse;
  int count = name.length();
 for (int i=0;i<count;i++){
    reverse[i]= name[count-i-1];
    cout<<i<<endl;
 }
  cout<<reverse<<endl;
  return 0;
}
5
  • This is the undefined behaviour. reverse.size() is 0. Use reverse.push_back(name[count-i-1]). Commented May 4, 2021 at 5:10
  • what does that mean sorry i m sorta starting out Commented May 4, 2021 at 5:12
  • Thanks reverse.push_back(name[count-i-1]) works, i didnt know i could use pushback in string and thought it was only for vectors Commented May 4, 2021 at 5:23
  • You could also use reverse.resize(name.length());. Commented May 4, 2021 at 5:26
  • std::string can take iterators for constructor. Consider declaring string reverse(name.crbegin(), name.crend());. Commented May 4, 2021 at 5:49

3 Answers 3

2

You can easily fix your original code by changing 1 line of code

This is the WRONG code (Original Code) :

reverse[i]= name[count-i-1]; // WRONG -- ORIGINAL code inside the for loop

This is the CORRECT code (New Code):

reverse += name[count-i-1]; // CORRECT code

Here is the full and correct code:

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

int main(){
  string name = "dog";
  string reverse;
  int count = name.length();
 for (int i=0;i<count;i++){
    reverse += name[count-i-1];  // I fixed it here. It works now.
    cout<<reverse[i]<<endl;
 }
  cout<<reverse<<endl;
  return 0;
}

I have tested and verified that it works now.

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

3 Comments

Yes, this is more like it. +1 (I'd remove #include <stdio.h> though. It's not needed)
Thank you so much for helping with that code. I've learned something new today. :-)
OK. I've removed #include <stdio.h> as it's not needed. Thanks to you and all others for helping me to improve this code for the OP. Cheers. :-)
2

The string reverse is 0 characters long so when you try assigning characters to it using operator[], you write out of bounds, making your program have undefined behavior.

One way to solve the problem is to create it with the same length as name:

string reverse(name.size(),' ');

or, to instead leave it as is, and add characters to it:

string reverse;
reverse.reserve(name.size()); // reserve space (may make it a little faster)

for (int i=0;i<count;i++){
    reverse.emplace_back(name[count-i-1]); // or push_back
}

Another option could be to use std::copy and copy it in reverse using the std::strings reverse iterators:

#include <algorithm>
#include <iterator>

std::copy(name.rbegin(), name.rend(), std::back_inserter(reverse));

or, if you don't need to keep the original content in name, you could reverse it directly:

#include <algorithm>

std::reverse(name.begin(), name.end()); // reverses name itself

Comments

-2

You want to set up your forLoop backwards. Do for( int i = ( count-1 ) ; i >= 0 ; i-- ){...}

Edit: initialize reverse to "" and simplify the inside of the new for loop to be reverse = ( reverse + name.at(i) ) ; and only do the final cout after the loop finishes (get rid of the inside one)

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.