Reversing a string 'in place'

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to reverse a string in place. Basically not creating extra memory to reverse the string. I think I have come up with a solution but keep getting compiler errors. <br/> <br/>
<div style="color:Black;background-color:White
<div style="color:Black;background-color:White
<pre lang="x-c# static string reverseStr(string str)
{
int lastIndex = str.Length - 1;
/*This is a method to reverse a string in Place*/
for (int i = 0; i < lastIndex; i++)
{
char temp = str;
str = str[lastIndex];
str[lastIndex] = temp;
lastIndex--;
}
return str;
}[/code]
<br/>
<br/> <br/>
But I keep getting an error where I attempt to assign a new value to an already occupied index which says: <br/> <br/> Error    1    Property or indexer string.this[int] cannot be assigned to -- it is read only<br/> Error    2    Property or indexer string.this[int] cannot be assigned to -- it is read only    <br/> <br/> Why cant I assign a new value there??<br/> <br/>

View the full article
 
Back
Top