#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
}
|