Overload string reversal function.

#include <iostream>
#include <cstring>
using namespace std;
void reverseString(char *s); // reverse string in place
void reverseString(char *in, char *out); // put reversal into out
int main()
{
char s1[80], s2[80];
strcpy(s1, "This is a test");
reverseString(s1, s2);
cout << s2 << endl;
reverseString(s1);
cout << s1 << endl;
return 0;
}
// Reverse string, put result in s.
void reverseString(char *s)
{
char temp[80];
int i, j;
for(i=strlen(s)-1, j=0; i>=0; i--, j++)
temp[j] = s[ i ];
temp[j] = '\0'; // null terminate result
strcpy(s, temp);
}
// Reverse string, put result into out.
void reverseString(char *in, char *out)
{
int i, j;
for(i=strlen(in)-1, j=0; i>=0; i--, j++)
out[j] = in[ i ];
out[j] = '\0'; // null terminate result
}
Related examples in the same category
1. | Wrap char pointer to a String class | |  |
2. | Read string and output its length | |  |
3. | Declare a stack class for characters. | |  |
4. | Enters a character and outputs its octal, decimal, and hexadecimal code. | | |
5. | cin and cout work with char array | |  |
6. | Declares str just before it is needed | |  |
7. | Using C strings: cin,=.sync, getline | |  |
8. | Convert a number to a char then convert to upper case and lower case | | |
9. | Using functions islower, isupper, tolower, toupper | | |
10. | Using functions isdigit, isalpha, isalnum, and isxdigit | | |
11. | Using functions isspace, iscntrl, ispunct, isprint, isgraph | | |