Reverse a string

Hi i need to reverse a string in C++ and i already have the following code but i keep getting errors can somebody help me out?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

string omdraaien (string woord){
	for ( int i = 0; i < woord.length; i++ ){
		woord[i] = woord [(woord.length) - i];
	}
	return woord;
}

int main (){
	string woord;
	cout << "Voer een woord in" << endl;
	cin >> woord;
	cout << "Het omgedraaide woord is" << omdraaien (woord) << endl;
	return 0;
}
woord.length is a function. you missed the ()

and at line 7, your first pass through the loop will try to access woord[woord.length()] which will be a problem.

try woord[i] = woord [(woord.length) - i - 1];
Last edited on
One more thing to consider is this line :
woord[i] = woord [(woord.length()) - i - 1];
You overwrite the woord array replacing characters of the original string.
how can i overcome that?
1
2
string omdraaien (string woord){
string droow;


droow[i] = woord [(woord.length) - i];

return droow;
1
2
3
4
5
6
7
8
9
10
string omdraaien(string woord)
{
	string temp = woord;

	for (int i = 0; i < woord.length(); i++)
	{
		temp[i] = woord[(woord.length()) - i - 1];
	}
	return temp;
}


When i do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

string omdraaien (string woord){
	string droow;
	for ( int i = 0; i < woord.length(); i++ ){
		droow[i] = woord [(woord.length()) - i];
	}
	return droow;
}

int main (){
	string woord;
	cout << "Voer een woord in" << endl;
	cin >> woord;
	cout << "Het omgedraaide woord is " << omdraaien (woord) << endl;
	return 0;
}
\

i get no output :/
@ konstance (17)
that work but can you explain why you did string temp = woord; and that worked?
you missed the -1 in line 8.
@ Jaybob66 even if i enter -1 it doesn't give me the reverse..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <algorithm>

std::string omdraaien( std::string woord ){

    const std::size_t len = woord.size() ; // or woord.length()

    // swap the first char with the last, second with last but one, etc.
	for ( std::size_t i = 0; i < len/2; ++i ){
		std::swap( woord[i], woord[ len-i - 1 ] ); // the last char is at position len-1
	}
	return woord;
}

int main (){

	std::string woord;
	std::cout << "Voer een woord in: " ;
	std::cin >> woord;
	std::cout << "Het omgedraaide woord is: " << omdraaien(woord) << '\n' ;
}
1
2
3
4
5
6
7
string omdraaien (string woord){
	string droow;
	for ( int i = 0; i < woord.length(); i++ ){
		droow[i] = woord [(woord.length()) - i -1];
	}
	return droow;
}


droow when created is an empty string with zero length.Trying to access it like droow[i] is a <<string subscript out of range>> error.
@ konstance
So what you did if i understand correctly is made a copy of the string word in order that this problem wouldn't occur?
ah yes, i see my mistake, you are correct, droow hass no length so the assign fails. konstance's solution makes sure droow has enough character space for the answer.
yup exactly :)
Okay thanks! But i was thinking isn't there a way to make do this with calling by reference / value?
There are several ways to do this.Do you have something specific in mind?
Topic archived. No new replies allowed.