Jump to content
  • Advertisement
Sign in to follow this  
SSJCORY

String reversal?

This topic is 5471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why doesn't this work?
#include<iostream>
#include<string>
using namespace std;
void reverseString(string input){
	string output = "";
	int k = 0;
	for(int i = input.length(); i >= 0;i--){
		output[k] = input.at(i);
		k++;
	}
	cout<<output;
}
int main(){
	reverseString("Cory");
	return 0;
}

I get abnormal program termination. What the hell does that mean? When I run through the debugger it stopps on the pop esi line. Whatever that means. IS that the assembly code or something ? If this isn't the solution then how might one output the reverse a string?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Why doesn't this work?

A string of length 5 ends at index 4.

Quote:
I get abnormal program termination. What the hell does that mean?


Uncaught exception. Bounds-checking done by std::string::at().

Quote:
If this isn't the solution then how might one output the reverse a string?


Use reverse iterators, or the appropriate standard algorithm?

Share this post


Link to post
Share on other sites
I tried doing instead of input.length() using input.length() - 1. It doesn't work. :(

First-chance exception in String Bender2k.exe: 0xC0000005: Access Violation.
The program 'C:\Documents and Settings\Cory Fisher\Debug\String Bender2k.exe' has exited with code 0 (0x0).

Share this post


Link to post
Share on other sites
Other than the errors Fruny already pointed out, you are assigning values to output[k] for arbitrary values of k, without ever allocating any room for it (the length of output is 0).

Share this post


Link to post
Share on other sites
The output string has a length of 0 and you're trying to stick characters past its bounds: output[k]. You should be reserving space in the output string that's the size of the input string.

Share this post


Link to post
Share on other sites
...
That's just an example of how to use it. It can be used with any standard container that supports bidirection iterators...ie: list, vector, string, or deque.

Share this post


Link to post
Share on other sites
Two choices

mutative version:

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

int main() {

std::string s("HELLO");

std::reverse(s.begin(), s.end());

std::cout << s << std::endl;

return 0;

}



non-mutative:

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

int main() {

std::string s("HELLO");

std::string new_s;

std::reverse_copy(s.begin(), s.end(),
std::back_inserter(new_s));

std::cout << new_s << std::endl;

return 0;

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!