Hi! I have just started programming, and I have a question for my assignment: When the input is ABCDE, i want the output to be EDCBA. But for some reason I cant understand he output i get is: EDCDE My code is: " for (int i = 0; i < b; i=i++) { char temp1 = a; a = a[(b-1)-i]; a[(b-1)-i] = temp1; cout << a ; " b is the word... Please help me Thank you!
> i=i++ Um, no. Either use i++ on its own or use i=i+1. Try as a test displaying the values of i and (b-1)-i in the loop instead. What values do you expect? What values do you get? Does that explain why you get EDCDE?
how does this grab ya? Code: #include <iostream> using namespace std; int main(){ char word[5]={'b','i','r','d','s'}; char temp1[5]; for (int i = 5; i > -1; i--) { char temp1 = word[i]; word[i] = temp1; cout<<word[i]; } cout<<endl; } hope it helps. i think this is what xpi0t0s was saying. you just turn the signs in the for loop, and make everything opposite. also i the for loop you put i=i++, i++ will work fine ^^
Did you run that code cpulocksmith? What did word contain after it finished? What's the point of word = word; ?
Code: int size = 5; char value1[size] = {'a','b','c','d','e'}; char value2[size]; for(int i = 0; i < size; i++) { value2[i] = value1[i]; } for(int j = size - 1; j > -1; j--) { cout << value[j]; } don't have my compiler on this cpu so didnt test it but unless i'm missing something it should work correct me if i'm wrong
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?
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
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; a= a[(b-1)-i]; a[(b-1)-i] = temp1;
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...)
What you are doing gives this result if you print temp1 and a in that loop temp1: abcba a: 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='\0';</p> <p> for (int i = 0; i < b; i++)</p> <p> {</p> <p> temp1 = a[b-i-1];</p> <p> cout<<temp1;</p> <p> }</p> <p>
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++)
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 << endl;