0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
OP:
> When the input is ABCDE, i want the output to be EDCBA

So do you need to reverse the string at all, or are you just outputting a string in reverse order?
Did you try what I suggested earlier - displaying the loop variables?

Ryzer's example won't work because value[] doesn't exist. But what I don't understand about it is what value2[] is for. This seems to be an exact duplicate of value1[], not reversed or anything. The next loop then displays value2[] in reverse order. So why doesn't it just display value1[] in reverse order?
0
vsachar's Avatar, Join Date: Feb 2009
Go4Expert Member
this just prints the string s in reverse.


Code: C++

cout<<"enter size of string:";
cin>>n;
cout<<"enter string:";
cin>>s;
for (int i = 0; i < n; i++)
{
cout<<s[n-i-1];
}



if you meant that the variable b is the string i don't think you should use it in the loop.
hope this helped
0
vsachar's Avatar, Join Date: Feb 2009
Go4Expert Member
sorry bout the paragraph tags
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by vsachar View Post
sorry bout the paragraph tags
Removed for you.
0
j+e's Avatar, Join Date: Feb 2009
j+e
Light Poster
I have already defined a function that counts how many characters, that is b. So what I should do is to take this input (regardless of length) and reverse the characters. Rysers example work just if you wrote the input in main.

So how can I use what I already have? I feel that I am really close, just that I get EDCDE instead of EDCBA:

void reverse(char a[], int b)
{
for (int i = 0; i < 5; i=i++)
{

char temp1 = a[i];
a[i]= a[(b-1)-i];
a[(b-1)-i] = temp1;
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Well, what about what I suggested? Did you display the values of i and (b-1)-i, and did they match what you expected?

Of course I could just debug it for you and give you the answer but you learn nothing that way. This way you learn to debug, and since 90% of programming is debugging this is by far the much greater skill. Any code monkey can knock out code but it takes skill to debug.

I see in fact you've completely ignored my previous post because you've still got "i=i++". Perhaps you think I don't know what I'm talking about (hint: eyes left, see what it says under my nick. Maybe I do...)
0
vsachar's Avatar, Join Date: Feb 2009
Go4Expert Member
What you are doing gives this result if you print temp1 and a[i] in that loop

temp1: abcba
a[i]: ebcbe

You are in no way reversing an array.

Next time, please try to write down in simple english what you want to do (meaning an algorithm) and then try again. Do not simply try to use bits and pieces from everywhere such as exchange used in sorting. No matter, here's a better way:


Code: C++
</p>
<p> a[b]='\0';</p>
<p> for (int i = 0; i < b; i++)</p>
<p> {</p>
<p> temp1[i] = a[b-i-1];</p>
<p> cout<<temp1[i];</p>
<p> }</p>
<p>
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
vsachar, Please try to keep HTML out when you paste codes
0
j+e's Avatar, Join Date: Feb 2009
j+e
Light Poster
To xpi0t0s:

I tried to do what you said. I printed out i and (b-1-i), what I got was

i = 0, b-1-i = 4
i = 1, b-1-i = 3
i = 2, b-1-i = 2
i = 3, b-1-i = 1
i = 4, b-1-i = 0

And that is what I want, because this I suppose would reverse all the letters. So I don't understand what I am doing wrong.

My teacher says that i++ is the same as i = i+1, therefore I didn't change.

But I have tried and tried now, and I don't see what is wrong, so maybe you could give me another hint?

just to correct myself om my last reply: for (int i = 0; i < b; i++)
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Yes, i++ is the same as i=i+1, but it's not the same as i=i++, which is what you had.

Try changing the last line to this, see if you can spot what's happening.
cout << a << endl << a[i] << endl;