User-Function not returning the new value

when i use the following code:

String Reverse(String c)
{
String temp;
String temp2;
int tempsize;



tempsize = c.length();

while(tempsize >= 0)
{

temp = c.substr(tempsize,1);
temp2 += temp;
tempsize--;
}
c = temp2;


return c;
}


the new value for string c does not return to the main body of the program
does anyone know how to fix this?
This code works correctly(if not cavil to implementation)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>
using namespace std;

string Reverse(string c)
{
	string temp;
	string temp2;
	int tempsize;

	tempsize = c.length();

	while(tempsize >= 0)
	{
		temp = c.substr(tempsize,1);
		temp2 += temp;
		tempsize--;
	}
	c = temp2;
	return c;
}

int main()
{
	cout << Reverse(string("abcd")) << endl;
	return 0;
}


But here i use STL string class. Maybe your String is different and not properly designed?
i use the Lawrenceville Press string library and i have had problems with it before, so it could be that the string class is not properly constructed in it. i guess i should try and find a diffrent one. Thx for the help.btw if you could tell me where i could get the string library file your using it would be very helpful because i can't seem to find one on the net.
Last edited on
The C++ Standard Library already comes with the string class that melkiy used. Just make sure you're using a decent compiler and dont forget to #include<string>
Last edited on
Topic archived. No new replies allowed.