Trying to reverse a string...

It's stored in an array

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 937 Views - Last Post: 28 May 2009 - 08:29 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Trying to reverse a string...

Posted 28 May 2009 - 04:53 PM

So this continues off of the code supplied to me to demonstrate XOR and when I try to decode it, it comes out reversed, so I decided to try and reverse the output to get the correct output...
Any help would be greatly appreciated.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
  char string[20] = "the man ran jumped";
  char key[20] = "ABCEDFGHIJLKMNOPZY";
  for(int x=0; x <= 17; x++)
  {
	string[x]=string[x]^key[x];
	cout << string[x];
  }
  cout << endl;
  for(int x = 17; x >= 0; x--) {
	string[x] = key[x]^string[x];
	cout << string[x];
	string::reverse_iterator RevIt;
	for(RevIt = string[x].begin(); RevIt < string[x].rend(); RevIt++) {
		cout << *RevIt; 
	} 
	}
  cout << endl;
  return 0;
}


Errors are as follow::
/home/ingenioushax/Desktop/xor.cpp: In function ‘int main()’:
/home/ingenioushax/Desktop/xor.cpp:19: error: request for member ‘begin’ in ‘string[x]’, which is of non-class type ‘char’
/home/ingenioushax/Desktop/xor.cpp:19: error: request for member ‘rend’ in ‘string[x]’, which is of non-class type ‘char’

Do I have to typecast these chars or?

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to reverse a string...

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1153
  • View blog
  • Posts: 7,166
  • Joined: 07-September 06

Re: Trying to reverse a string...

Posted 28 May 2009 - 05:24 PM

The problem is you are trying to treat your array of characters as an array of classes, they are very different things. You should probably not have a variable called string when you are using the string class in your program.
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Trying to reverse a string...

Posted 28 May 2009 - 06:00 PM

Ok, I can change that but that doesn't help me in anyway with solving this problem.
Was This Post Helpful? 0
  • +
  • -

#4 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Trying to reverse a string...

Posted 28 May 2009 - 06:09 PM

hope this helps dude :)
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: Trying to reverse a string...

Posted 28 May 2009 - 06:17 PM

Though it isn't creating an error, I would like to point out the following :

  for(int x=0; x <= 17; x++) {
	string[x]=string[x]^key[x];
	cout << string[x];
  }
  cout << endl;
  for(int x = 17; x >= 0; x--) {



You declare x in each for statement. You really should only declare your variables at the begging of the function, & then use them through out the code.

int main(void) {
  int x=0;
  for(x=17; x >= 0; x--) {
...


Was This Post Helpful? 0
  • +
  • -

#6 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Trying to reverse a string...

Posted 28 May 2009 - 06:24 PM

Compilers optimize such things out.

This post has been edited by Dantheman: 28 May 2009 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: Trying to reverse a string...

Posted 28 May 2009 - 06:26 PM

View PostDantheman, on 28 May, 2009 - 07:24 PM, said:

Compilers optimize such things out.

So it's ok to write crap code, just so long as the machine is going to fix it later?

Good coding standards are easier taught than changed.
Was This Post Helpful? 0
  • +
  • -

#8 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Trying to reverse a string...

Posted 28 May 2009 - 06:34 PM

View Postno2pencil, on 28 May, 2009 - 05:26 PM, said:

View PostDantheman, on 28 May, 2009 - 07:24 PM, said:

Compilers optimize such things out.

So it's ok to write crap code, just so long as the machine is going to fix it later?

Good coding standards are easier taught than changed.

You consider such a minor detail as "crap code"? That's only in your opinion.

Declaring variables before using them is not a good coding standard - it's a bad coding standard.

That's why we have high-level languages - write a clear maintainable code, and let compiler do the dirty job. Moving into the direction of Assembly is going the wrong way.

This post has been edited by Dantheman: 28 May 2009 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: Trying to reverse a string...

Posted 28 May 2009 - 06:40 PM

I consider redefining variables a horrible idea & very misleading. Regardless if there are automatic compiler optimizations that will correct it, if you allow yourself to get lax & used to writing garbage code that the compiler constantly fixes, then the moment you work with tools that don't correct your mistakes, you are not going to recognize the error.

It's a poor coding practice. I'm sorry that you disagree with me.

With all respect to IngeniousHax, I would suggest against it. However for the sake of the topic, it isn't (as I pointed out) an actual error, only my suggestion. Sorry for derailing the topic.
Was This Post Helpful? 0
  • +
  • -

#10 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Trying to reverse a string...

Posted 28 May 2009 - 06:45 PM

View Postno2pencil, on 28 May, 2009 - 05:40 PM, said:

I consider redefining variables a horrible idea & very misleading. Regardless if there are automatic compiler optimizations that will correct it, if you allow yourself to get lax & used to writing garbage code that the compiler constantly fixes, then the moment you work with tools that don't correct your mistakes, you are not going to recognize the error.

It's a poor coding practice. I'm sorry that you disagree with me.


Well, everybody is entitled to their opinion. I'm not here to change anybody's view, I'm only sharing mine.

I, personally, have no problem with a compiler fixing my code, because guess what... compiler will fix my code anyway. Such basic optimizations as Common Subexpression Elimination are guaranteed to be applied to my code. Therefore, I will not bother making my code less readable by declaring variables before their usage - now that's a garbage code.

Yes, I might fiddle with the assembler from time to take, but that doesn't mean that I should be thinking in Assembly everytime I write code - that would be painful and would defeat the purpose of a high-level language.

Is it misleading? Of course not! People declare counters inside loops all the time. In fact, I consider a declaration of a temporary counter in the higher scope as misleading. When I read the code and see that counter being declared in the main scope, I have to keep that variable in my head at all times, because I think it has a major significance. Even after the first loop is over, I have to remember the value of that counter, because if it's declared in the main scope it will be used somewhere again. But when I see it being declared inside the loop, I know that I can forget about it after the loop is done.

This post has been edited by Dantheman: 28 May 2009 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#11 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Trying to reverse a string...

Posted 28 May 2009 - 07:11 PM

Sorry, I didn't even read the topic. Don't want to start a flamewar.

Ingenious, you say your string comes out reverse. The string itself has the correct order. It's just that, your loop goes from 17 to 0. Make it from 0 to 17 and you should be good to go.

This post has been edited by Dantheman: 28 May 2009 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: Trying to reverse a string...

Posted 28 May 2009 - 07:13 PM

View PostDantheman, on 28 May, 2009 - 07:45 PM, said:

Is it misleading? Of course not! People declare counters inside loops all the time.

He declared x twice.... It could be misleading.

View PostDantheman, on 28 May, 2009 - 07:45 PM, said:

When I read the code and see that counter being declared in the main scope, I have to keep that variable in my head at all times, because I think it has a major significance. Even after the first loop is over, I have to remember the value of that counter, because if it's declared in the main scope it will be used somewhere again. But when I see it being declared inside the loop, I know that I can forget about it after the loop is done.


Right... & now that the 1st for loop is done... we've gotten so sloppy about being bothered to remember variables, that what the heck, lets declare it again. I mean your above statement alone sort of proves my point. You want to be lax & let the compiler do the dirty work, you've stated that, you've proven that. That's why I said that's crap coding. Put in a little extra time at the pencil & paper stage to have an understanding of your own code, & you won't have that problem.

Plan -> Process -> Results

Standards.
Was This Post Helpful? 0
  • +
  • -

#13 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Trying to reverse a string...

Posted 28 May 2009 - 07:18 PM

View Postno2pencil, on 28 May, 2009 - 06:13 PM, said:

View PostDantheman, on 28 May, 2009 - 07:45 PM, said:

Is it misleading? Of course not! People declare counters inside loops all the time.

He declared x twice.... It could be misleading.


Oh, you have a beef with him using the same name for the variable. I can see where you're coming from. I guess it's just a matter of personal experience. If you're used to seeing counters being declared outside of the loops (especially if you have a C background) then I can see how that can be misleading. I'm just so used to seeing counters being declared inside loops that I don't even pay attention to their names.
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: Trying to reverse a string...

Posted 28 May 2009 - 07:22 PM

View PostDantheman, on 28 May, 2009 - 08:18 PM, said:

I guess it's just a matter of personal experience.

I thought we already had that understand. That's the point I was trying to get across from when I even brought it up. Somehow I keep ending up on this damn soap box.

View PostDantheman, on 28 May, 2009 - 08:18 PM, said:

If you're used to seeing counters being declared outside of the loops (especially if you have a C background) then I can see how that can be misleading.

Bingo.

Take a guess at who codes in C :) I have very little C++ coding experience. I've gotten used to not relying on the next compiler to perform the same as the previous.
Was This Post Helpful? 0
  • +
  • -

#15 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Trying to reverse a string...

Posted 28 May 2009 - 07:24 PM

View Postno2pencil, on 28 May, 2009 - 06:13 PM, said:

Right... & now that the 1st for loop is done... we've gotten so sloppy about being bothered to remember variables, that what the heck, lets declare it again. I mean your above statement alone sort of proves my point. You want to be lax & let the compiler do the dirty work, you've stated that, you've proven that. That's why I said that's crap coding. Put in a little extra time at the pencil & paper stage to have an understanding of your own code, & you won't have that problem.

Plan -> Process -> Results

Standards.


We have a different definition of "crappy code".

If I have 15 variables to hold in my head, I'd rather not hold 16th variable, especially if that variable is completely useless, misleading and results in 0 penalty. Focus on the problem itself, not useless details. After all, that's why we move away from Assembly.

This post has been edited by Dantheman: 28 May 2009 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2