Why doesn't this string reverse work with the space for this particular string?

S

sherifffruitfly

Hi all,

I just wrote this up real quick, and it seems to work with a bunch of
input strings (with and without spaces) except for the one in the code
below:

string input = "this is a string";
int length = input.Length-1;
char [] reverseMe = input.ToCharArray();
char temp;

for (int i = 0; i<length / 2; i++)
{
temp = reverseMe;
reverseMe = reverseMe[length - i];
reverseMe[length - i] = temp;
}

string done = new string(reverseMe);
MessageBox.Show(done);

Expected output: gnirts a si siht
Actual output: gnirts asi siht

What's with the missing space between the "is" and the "a"?


thanks in advance,

cdj
 
J

John Duval

Hi all,

I just wrote this up real quick, and it seems to work with a bunch of
input strings (with and without spaces) except for the one in the code
below:

string input = "this is a string";
int length = input.Length-1;
char [] reverseMe = input.ToCharArray();
char temp;

for (int i = 0; i<length / 2; i++)
{
temp = reverseMe;
reverseMe = reverseMe[length - i];
reverseMe[length - i] = temp;
}

string done = new string(reverseMe);
MessageBox.Show(done);

Expected output: gnirts a si siht
Actual output: gnirts asi siht

What's with the missing space between the "is" and the "a"?

thanks in advance,

cdj


It works with some strings but not others? How ODD. Try it with this
string: ">>>>>>>AB<<<<<<<", it should become clear.

John
 
P

Peter Duniho

sherifffruitfly said:
Hi all,

I just wrote this up real quick, and it seems to work with a bunch of
input strings (with and without spaces) except for the one in the code
below:

string input = "this is a string";
int length = input.Length-1;
char [] reverseMe = input.ToCharArray();
char temp;

for (int i = 0; i<length / 2; i++)
{
temp = reverseMe;
reverseMe = reverseMe[length - i];
reverseMe[length - i] = temp;
}

string done = new string(reverseMe);
MessageBox.Show(done);

Expected output: gnirts a si siht
Actual output: gnirts asi siht

What's with the missing space between the "is" and the "a"?


You've got an off-by-one error, because you subtract one from the Length
of the original string.

That said, your code is inefficient, because it's swapping instead of
just copying, requiring an intermediate variable and copy of the data.
It takes 50% more moving of data than this:

string input = "this is a string";
StringBuilder builder = new StringBuilder(input.Length);

for (int ich = input.Length - 1; ich >= 0; ich--)
{
builder.Append(input[ich]);
}

string done = builder.ToString();
MessageBox.Show(done);

If you want more readable:

string input = "this is a string";
char[] rgch = input.ToCharArray();

Array.Reverse(rgch);

string done = new string(rgch);
MessageBox.Show(done);

Pete
 
J

Jon Skeet [C# MVP]

sherifffruitfly said:
I just wrote this up real quick, and it seems to work with a bunch of
input strings (with and without spaces) except for the one in the code
below:

string input = "this is a string";
int length = input.Length-1;
char [] reverseMe = input.ToCharArray();
char temp;

for (int i = 0; i<length / 2; i++)
{
temp = reverseMe;
reverseMe = reverseMe[length - i];
reverseMe[length - i] = temp;
}

string done = new string(reverseMe);
MessageBox.Show(done);

Expected output: gnirts a si siht
Actual output: gnirts asi siht

What's with the missing space between the "is" and the "a"?


You haven't got quite enough iterations of the loop. Change it to
for (int i = 0; i < (length+1) / 2; i++)

and it'll be fine.

Think about the situation where you've got the string "abcd". length=3,
so length/3=1, so it'll only do a single iteration.

One point: it took a little while to work this out due to setting
length to input.Length-1. That's not an obvious meaning of "length"!

You should also consider using Array.Reverse instead...
 
S

sherifffruitfly

sherifffruitfly said:
I just wrote this up real quick, and it seems to work with a bunch of
input strings (with and without spaces) except for the one in the code
below:
string input = "this is a string";
int length = input.Length-1;
char [] reverseMe = input.ToCharArray();
char temp;
for (int i = 0; i<length / 2; i++)
{
temp = reverseMe;
reverseMe = reverseMe[length - i];
reverseMe[length - i] = temp;
}

string done = new string(reverseMe);
MessageBox.Show(done);
Expected output: gnirts a si siht
Actual output: gnirts asi siht
What's with the missing space between the "is" and the "a"?

One point: it took a little while to work this out due to setting
length to input.Length-1. That's not an obvious meaning of "length"!

Yup - it was my too-quick attempt to deal with zero-based arrays. I
see the error now - thanks!
 
S

sherifffruitfly

I just wrote this up real quick, and it seems to work with a bunch of
input strings (with and without spaces) except for the one in the code
below:
string input = "this is a string";
int length = input.Length-1;
char [] reverseMe = input.ToCharArray();
char temp;
for (int i = 0; i<length / 2; i++)
{
temp = reverseMe;
reverseMe = reverseMe[length - i];
reverseMe[length - i] = temp;
}

string done = new string(reverseMe);
MessageBox.Show(done);
Expected output: gnirts a si siht
Actual output: gnirts asi siht

It works with some strings but not others? How ODD. Try it with this
string: ">>>>>>>AB<<<<<<<", it should become clear.

John


Yup - that's much more illustrative of the error - thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top