Originally Posted by
U79
Can you tell me exactly what those are? I'm really sorry, but I don't really know the terms.
Look at this example:
Code:
int foo(int bar)
{
...
}
The return type in the above example is red. It states (in this example) that the function returns an integer value. The argument/parameter type in the example is in blue. It states that the function expects a single parameter/argument of type integer to be passed into the function.
Code:
#include <iostream.h>
If possible you should be using the iostream header and not iostream.h. This will bring up the issue of namespaces however and at this stage it is perhaps easiest to deal with this by simply putting using namespace std; underneath the #include line.
You should always explicitly put that int return type for the main function.
Code:
reversed_string[21] = reverse_string(original_string[21]);
You should not be calling your function like that. Character strings (character arrays) are declared like reversed_string[21] but when you pass them into functions, you typically just use the array name like reverse_string(original_string). Arrays are indexed starting from 0, so an array holding 21 characters has a valid index from 0 through 20 inclusive. Outside of declaring an array, the syntax you use in the above sample is used to access individual elements of the array (the individual characters in this case) thus reversed_string[0] represents the first character and reversed_string[21] represents the 22nd character which is an invalid index. The same goes for your use of the original_string[21] in the above example.
Code:
char original_string[21], reversed_string[21];
for (x = 0; x < 21; x++)
reversed_string[x] = original_string[21-x];
cin.get(original_string, 21);
I would strongly suggest you use a const int variable instead of a hardcoded value here. If in the future you need to change the length of the array, you change it in one single place instead of having to search through your code and replace every 21 with a different value, i.e.:
Code:
const int STR_SIZE=21;
char original_string[STR_SIZE], reversed_string[STR_SIZE];
...
etc, etc...
As stated previously (by nvoigt), your method of reversing the string should take into account the actual length of data stored in your original_string variable and not the full size of the array itself. You also need to deal with the NULL terminating character, copying it to the correct position and not reversing it by mistake. To deal with these issues, you need to find the length of data in the original_string by looping through it and finding the NULL character, your looping variable at this point will give you the index of the NULL character (and also the length). Once you've got the length, you copy the original string (starting at an index of length minus one and moving towards zero) into the reversed string (starting at index 0 and incrementing up). At the end put a NULL character at the end of the reversed string.
You might still have some issues with how you are passing/returning your variables but here you have some suggestions to think about in the meantime.