I can't reverse the inputted word...
Here is my code... Please correct it...

Sample Output:

Type word(s): hello
The reverse is: 'olleh'

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char orig [300];
char rev [300];
int len;
void original ()
		cout << "\n\n\tType word(s): ";
		cin.getline (orig,300);
void reversed ()
		int x = 0;
		int y = len;
		for (x=0;x<y;x++)
				rev [x]=orig [y-x];
void main ()
		clrscr ();
		original ();
		len = strlen (orig);
		reversed ();
		cout << "\n\n\tThe reverse is: '" << rev << "'.";
		getch ();

Edited by aladar04: n/a

7 Years
Discussion Span
Last Post by aladar04

First thanks to add code tags to your post.
Second, (and I know this may sound harsh): your code is the ideal example of how one shouldn't program it, but no fear, we're here to help you and give you a better insight in how you should do it.

To begin I'll start to comment on some bad things in your code:

  • You've multiple include directives in your code, but they aren't all necessary (you can leave out half of it).
  • You make use of deprecated header files, are you maybe using an ancient compiler? (anyway, here's a link which will get you started getting a decent compiler and migrating your code to standard C++: http://siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html)
  • You use void main(), per standard you should use int main() (also check out the link in my previous post).
  • You're also making use of global variables, agreed they can be useful in very rare circumstances, but for a simple program like this you better avoid using them, not only because your functions are making use of variables which are defined outside them, but also because those variables' values could be accidentally changed, causing your program to fail.

Could you maybe first of all improve your code, keeping the above list in the back of your mind?
(a little try, and even the smallest effort is okay for me, but if you want to impress me (and others), and are willing to improve yourself, then just try to do it as good as you can, then post down your code again and then we'll talk about how we can get the whole thing running smoothly).

Sure I can just give you the code, but I don't do this for several reasons:

  • It's against the Daniweb policy (link 1; link 2).
  • I think 'Daniweb': I rather provide paths to the solution, and not the entire solution itself (yes, I have to admit that I sometimes post a solution, but then I try to give as much side comments as possible (in the hope the OP will read it and (of course) learn from it)).
  • It's against my policy: I'm almost completely self-taught, and personally I think this is the best way to be succesful.
    I even remember those bad old days, when it was really trial-and-error, but those bad old days are the days where your adventure of gathering experience with programming starts.
    (Just: 'starts', I'm not sure whether this adventure can really end).

Edited by mvmalderen: fix typos


I appreciate your concern tux4life. I would like to say sorry for using obsolete codes...

I'm just a beginner. I'm just following my professor's instruction to use the codes that he has taught us (and those are the codes I used in this post). Sorry, I forgot to mention that.

By the way, a friend helped me in solving this problem. He just told me to change

rev [x]=orig[y-x]



. As simple as that. :icon_cheesygrin:

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.